第一个例子 文件的复制import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Test1 { public static void main(String[] args) {
try {
FileReader a = new FileReader("F:\\234.txt");
FileWriter b = new FileWriter("D:\\565.txt");
int i = a.read();
while(i!=-1){
b.write(i);
i = a.read();

}
b.close();
a.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第二个例子 也是文件的复制
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Test1 { public static void main(String[] args) {
try {
FileReader a = new FileReader("F:\\234.txt");
BufferedReader b = new BufferedReader(a);
FileWriter c = new FileWriter("D:\\5666.txt");
BufferedWriter d = new BufferedWriter(c);
String i = b.readLine();
while(i!=null){
d.write(i);
d.newLine();
i=b.readLine();

}
d.close();
c.close();
b.close();
a.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}现在搞不懂一个问题,这2中用法有什么区别 那种方法更好一些??

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【peterandy0116】截止到2008-07-05 11:42:47的历史汇总数据(不包括此帖):
    发帖的总数量:8                        发帖的总分数:80                       
    结贴的总数量:8                        结贴的总分数:80                       
    无满意结贴数:4                        无满意结贴分:65                       
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:50.00 %               无满意结分率:81.25 %                  
    敬礼!
      

  2.   

    另外第二个代码Write的close()前  要加上flush()
      

  3.   

    建议你使用文件通道里的transferTo或者transgerFrom方法来做更快。这两个方法可以直接把文件从一个通道送到另一个通道,在程序中就没必要使用read和write方法了。
      

  4.   


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.nio.channels.FileChannel;public class FileCopy {
      public static void main(String[] args) {    File fromFile = new File("F:\\234.txt");     if(!fromFile.exists()) {
          System.out.printf("File to copy, %s, does not exist.",
                                           fromFile.getAbsolutePath());
          System.exit(1);
        }
        
        File toFile = new File("D:\\565.txt");     FileInputStream inFile = null;
        FileOutputStream outFile = null;
        try {
          inFile = new FileInputStream(fromFile); 
          outFile = new FileOutputStream(toFile);    } catch(FileNotFoundException e) {
          e.printStackTrace(System.err);
          assert false;
        }    FileChannel inChannel = inFile.getChannel();    
        FileChannel outChannel = outFile.getChannel();     try {
          int bytesWritten = 0;
          long byteCount = inChannel.size();
          while(bytesWritten<byteCount) {
            bytesWritten += inChannel.transferTo(bytesWritten, 
                                                 byteCount-bytesWritten,
                                                 outChannel); 
          }
          
          System.out.printf("File copy complete. %d bytes copied to %s%n",
                                         byteCount, toFile.getAbsolutePath());
          inFile.close();
          outFile.close();    } catch(IOException e) {
          e.printStackTrace(System.err);
          System.exit(1);
        }
        System.exit(0); 
      }
    }
      

  5.   

    实现文件copy的一个有效率的方法。
      

  6.   

    单纯复制文件可以使用 5楼 的方法,效率高。如果想自己通过输入输出流来完成复制,最好使用缓冲处理的 InputStream 和 OutputStream,这样把文件内容当作字节序列而不是字符序列处理效率高(因为省去了字符解码和编码的开销)且安全(如果用 Reader 和 Writer 复制非纯文本文件会出错儿)。