关于FileReader和BufferedReader的疑问,读取文件内容的区别??

peterandy0116 2008-07-05 11:42:51
第一个例子 文件的复制

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中用法有什么区别 那种方法更好一些??
...全文
745 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sagezk 2008-07-05
  • 打赏
  • 举报
回复
单纯复制文件可以使用 5楼 的方法,效率高。如果想自己通过输入输出流来完成复制,最好使用缓冲处理的 InputStream 和 OutputStream,这样把文件内容当作字节序列而不是字符序列处理效率高(因为省去了字符解码和编码的开销)且安全(如果用 Reader 和 Writer 复制非纯文本文件会出错儿)。
bajinggong 2008-07-05
  • 打赏
  • 举报
回复
实现文件copy的一个有效率的方法。
jingulang 2008-07-05
  • 打赏
  • 举报
回复
楼上贴出一大堆什么东西.......

bajinggong 2008-07-05
  • 打赏
  • 举报
回复

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);
}
}
bajinggong 2008-07-05
  • 打赏
  • 举报
回复
建议你使用文件通道里的transferTo或者transgerFrom方法来做更快。这两个方法可以直接把文件从一个通道送到另一个通道,在程序中就没必要使用read和write方法了。
jingulang 2008-07-05
  • 打赏
  • 举报
回复
另外第二个代码

Write的close()前 要加上flush()
jingulang 2008-07-05
  • 打赏
  • 举报
回复
第一个是处理单个字符 一个字符一个字符的读
第二个是处理整行字符串的啊 就是一行一行的读

goodadd 2008-07-05
  • 打赏
  • 举报
回复
一个用了缓存,一个没有用,当然用了的好

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧