4

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

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

谢谢。

4

4 回答 4

13

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

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

    while((i = fin.read()) != -1 ){
        fout.write(i);
    } 
于 2012-11-03T07:58:17.493 回答
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);

或者改成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();                                                  
    }                                                                           
} 
于 2012-11-03T07:58:00.690 回答
5

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

public static void copyFile( File from, File to ) throws IOException {
    Files.copy( from.toPath(), to.toPath() );
}
于 2012-11-03T08:08:31.217 回答
1

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

do {
    i = fin.read();
    if(i != -1)
    fout.write(i);
   } while (i != -1);
于 2012-11-03T08:04:35.563 回答