繁体   English   中英

在java中复制文件

[英]Copying files in java

你能告诉我为什么这个字符“ ÿ ”出现在我的输出文件的末尾。 (我用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);

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

谢谢。

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

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

    while((i = fin.read()) != -1 ){
        fout.write(i);
    } 

因为最后一个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);

或者改变do .. while在一段while .. do电话。

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

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();                                                  
    }                                                                           
} 

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

public static void copyFile( File from, File to ) throws IOException {
    Files.copy( from.toPath(), to.toPath() );
}

或者您可以在fout之前检查是否(i!= -1)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM