在java中复制文件

Ins*_*ing 4 java inputstream file fileinputstream

你能告诉我为什么这个字符" ÿ "出现在我的输出文件的末尾.(我用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);
Run Code Online (Sandbox Code Playgroud)

代码复制文件内容,但它以这个奇怪的字符结束.我是否必须包含整个代码?

谢谢.

Daw*_*ica 13

fin.read()当没有什么可读的时候,该方法返回-1; 但实际上你写的是-1 fout,即使它没有出现fin.它显示为ÿ角色.

编写循环以避免此问题的一种方法是

    while((i = fin.read()) != -1 ){
        fout.write(i);
    } 
Run Code Online (Sandbox Code Playgroud)

  • +1还要考虑使用`BufferedInputStream`和`BufferedOutputStream`来获得更好的性能. (2认同)

dan*_*dan 5

因为最后fin.read()不会读任何东西.根据JavaDoc,它将返回-1,因此你的fout.write(i)意愿将写入-1.你会做这样的事情来纠正这种行为:

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

或改变do .. while成一个while .. do呼叫.

我建议你也应该看看新的NIOAPI,它会比一次传输一个角色好得多.

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();                                                  
    }                                                                           
} 
Run Code Online (Sandbox Code Playgroud)


Sim*_*mon 5

尝试使用Java 7中引入的新Files类

public static void copyFile( File from, File to ) throws IOException {
    Files.copy( from.toPath(), to.toPath() );
}
Run Code Online (Sandbox Code Playgroud)