4

Would you please tell me why does this character " ÿ " appears at the end of my output file. (I use try/catch)

        File f1 = new File("C:/Users/NetBeansProjects/QuestionOne/input.txt");
        File f2 = new File("C:/Users/NetBeansProjects/QuestionOne/output.txt");
        fin = new FileInputStream(f1);
        fout = new FileOutputStream(f2);

        do {
            i = fin.read();
            fout.write(i);

        } while (i != -1);

The code copies the file content but it ends it with this strange character. Do I have to include the entire code?

Thanks.

  • 1
    Why re-invent the wheel? Apache Commons IO has a function for that. –  Nov 03 '12 at 08:22

4 Answers4

13

The method fin.read() returns -1 when there's nothing left to read; but you're actually writing that -1 to fout, even though it didn't occur in fin. It shows up as that ÿ character.

One way to write your loop to avoid this problem is

    while((i = fin.read()) != -1 ){
        fout.write(i);
    } 
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
5

Because the last fin.read() will not read anything. According to JavaDoc it will return -1, because of this your fout.write(i) will write that -1. You would do something like this, to correct this behavior:

do {
  i = fin.read();
  if (i>-1) //note the extra line
   fout.write(i);
} while (i != -1);

Or change the do .. while into a while .. do call.

I suggest you should also look at the new NIO API that will perform much better then transferring one character at a time.

File sourceFile = new File("C:/Users/NetBeansProjects/QuestionOne/input.txt");  
File destFile = new File("C:/Users/NetBeansProjects/QuestionOne/output.txt");   

FileChannel source = null;                                                      
FileChannel destination = null;                                                 

try {                                                                           
    if (!destFile.exists()) {                                                   
        destFile.createNewFile();                                               
    }                                                                           
    source = new FileInputStream(sourceFile).getChannel();                      
    destination = new FileOutputStream(destFile).getChannel();                  
    destination.transferFrom(source, 0, source.size());                         
} catch (IOException e) {                                                       
    System.err.println("Error while trying to transfer content");               
    //e.printStackTrace();                                                      
} finally {                                                                     
    try{                                                                        
    if (source != null)                                                         
        source.close();                                                         
    if (destination != null)                                                    
        destination.close();                                                    
    }catch(IOException e){                                                      
        System.err.println("Not able to close the channels");                   
        //e.printStackTrace();                                                  
    }                                                                           
} 
dan
  • 13,132
  • 3
  • 38
  • 49
5

try to use the new Files class introduced in Java 7

public static void copyFile( File from, File to ) throws IOException {
    Files.copy( from.toPath(), to.toPath() );
}
Simon
  • 17,223
  • 1
  • 19
  • 23
1

or you can simply check if(i != -1) before fout

do {
    i = fin.read();
    if(i != -1)
    fout.write(i);
   } while (i != -1);
user1759804
  • 73
  • 10