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.
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
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:
Code :
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...
Code :
catch (IOException e) {
//Better do something here, or you will never know an error occurred.
}
...at the least print the stack trace