转载请注明出处:BufferedReader、BufferedWriter_Mr_Leixiansheng的博客-CSDN博客
步骤:
1、创建好Buffer 对象
2、设置好读写路径
3、设置一个缓存String s
4、开始读写
5、关闭流
注意事项:
1、读写操作后一定要关闭流
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Text {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建BufferedReader、BufferedWriter对象
BufferedReader br = null;
BufferedWriter bw = null;
try {
//第一种指定路径方式
// FileReader fr = new FileReader("d:\\text.txt");
// FileWriter fw = new FileWriter("d:\\text3.txt");
//第二种设置读取路径
br = new BufferedReader(new FileReader("d:\\text.txt"));
bw = new BufferedWriter(new FileWriter("d:\\text3.txt"));
//缓存区,将读取的内容存放到s中
String s = " ";
while((s=br.readLine())!=null) {
//将s的内容写到bw
bw.write(s);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
//关闭流(必须有)
br.close();
bw.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}