Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Two characters trancated

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Two characters trancated

    Hi,

    I have following method that adds passed arrays of txt files to zip. The method was working fine until I added a condition if (in.read() != -1){} that checks if a file is empty and skip it.
    That works but, in every file added to a zip now first two characters on the first line are truncated like that
    0000234...
    P00000434...
    P00000254...

    instead of

    P00000234...
    P00000434...
    P00000254...

    Here is the method:

    public void compressFiles(String[] fileArray, String zipFileName){
            byte[] buf = new byte[1024];
            try {
               // Create the ZIP file
                String outFilename = zipFileName;
     
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
     
                // Compress the files
                for (int i=0; i < fileArray.length; i++) {
    	            FileInputStream in = new FileInputStream(fileArray[i]);
     
                    if (in.read() != -1){   // check if a file is not empty
                        // Add ZIP entry to output stream.
                        out.putNextEntry(new ZipEntry(fileArray[i]));
     
                        // Transfer bytes from the file to the ZIP file
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
     
                        // Complete the entry
                        out.closeEntry();
                    }
                    in.close();
                }
                // Complete the ZIP file
                out.close();
             }
             	catch (IOException e) {
     
             }
        }

    Any help is greatly appreciated.
    Thanks,
    Gena
    Last edited by gena_mak; July 9th, 2012 at 01:10 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Two characters trancated

    You are reading the first byte(s) of the file to check if it is empty or not. If not, you then start to read and write the file from that point on...in other words, your code skips the first bytes read due to your initial check. If it is not -1, you should write those bytes to the file.

    For future reference, please use the code tags.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Two characters trancated

    @copeg
    Please, help me understand. I am new to Java and this is really casual maintenance work I need to do.

    I kind of understand what is happening. I was not expecting that once the check is done one for a character, the method skips to the next byte/character and, why two bytes are truncated, not one?

    The method accepted 2 files as a file array. Then, enters into a for loop with first file then, created in stream, read the file into in stream and, check if it is empty or not by checking its read status. If it is empty then its -1 - skip close stream. Next file read into in stream, if not empty then, added to zip and copied ...

    If this if (in.read() != -1) makes the method to skip characters, what do I use then?

    Thanks

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Two characters trancated

    Calling in.read() reads the byte, but your code does nothing with the read byte. If there is a byte read, you should do something with it:

    byte firstByte = in.read();
    if ( firstByte == -1 ){
    //skip
    }else{
    ///write the first byte to the file
    ///Continue to read the rest of the file. 
    }

    And never, ever let exceptions fall through...

    catch (IOException e) {
    //Better do something here, or you will never know an error occurred.
    }

    ...at the least print the stack trace

Similar Threads

  1. [SOLVED] Strings and Characters
    By av8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 4th, 2011, 05:39 PM
  2. Strings and Characters
    By av8 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: July 3rd, 2011, 03:21 PM
  3. [SOLVED] characters per line in Eclipse
    By joon in forum Java IDEs
    Replies: 1
    Last Post: June 20th, 2011, 07:47 AM
  4. [SOLVED] Characters per line in Eclipse
    By joon in forum Java Theory & Questions
    Replies: 2
    Last Post: June 18th, 2011, 04:41 PM
  5. finding unescaped XML characters
    By diptee in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 18th, 2010, 04:51 AM