【问题标题】:Copying files in java在java中复制文件
【发布时间】:2012-10-23 19:38:22
【问题描述】:

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

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

谢谢。

【问题讨论】:

  • 为什么要重新发明轮子? Apache Commons IO 有一个功能。

标签: java file inputstream fileinputstream


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      或者您可以在 fout 之前简单地检查 if(i != -1)

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

      【讨论】:

        【解决方案4】:

        方法fin.read() 在没有可读取的内容时返回-1;但是您实际上是在将 -1 写入fout,即使它没有出现在fin 中。它显示为那个 ÿ 字符。

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

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

        【讨论】:

        • +1 还可以考虑使用BufferedInputStreamBufferedOutputStream 以获得更好的性能。
        猜你喜欢
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 1970-01-01
        • 2015-10-29
        • 2011-12-30
        • 2013-12-26
        • 1970-01-01
        • 2012-01-06
        相关资源
        最近更新 更多