BuffereReader继承于Reader,BufferedWriter继承于Writer,是字符缓冲流。
这两者常用的方法是:read(),读取单个字符;read(char[] cbuf,int off,int len),将字符读入数组的某一部分;readLine(),读取一个文本行;write(char[] cbuf,int off,int len),写入字符数组的某一部分;write(int c)写入单个字符;write(String s,int off,int len),写入字符串的某一部分;close(),关闭该流;
下面我们1通过一些实例代码对其进行学习:
①:进行文件的拷贝
@Test
public void t1() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("H:\\javaio\\copy01.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("H:\\javaio\\copy02.txt"));
// char[] cbuf = new char[1024];
// int len;
// while((len=br.read(cbuf)) != -1){
// bw.write(cbuf, 0, len);
// }
//BufferedReader提供了readLine方法,可以不再使用字节读取方式
String readline;
while((readline = br.readLine()) != null){
bw.write(readline);
bw.newLine();
}
bw.close();
br.close();
}
②:将文件追加到文件
@Test
public void t2() throws Exception{
BufferedWriter bw = new BufferedWriter(new FileWriter("H:\\javaio\\test.txt",true));
bw.write("我是测试用例!");
bw.close();
}
③:读取文件内容
@Test
public void t3() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("H:\\javaio\\test.txt"));
String readline;
while((readline = br.readLine()) != null){
System.out.println(readline);
}
br.close();
}
Java中io流的学习(一)File:https://blog.csdn.net/qq_41061437/article/details/81672859
Java中io流的学习(二)FileInputStream和FileOutputStream:https://blog.csdn.net/qq_41061437/article/details/81742175
Java中io流的学习(三)BuffereInputStream和BuffereOutputStream:https://blog.csdn.net/qq_41061437/article/details/81743522
Java中io流的学习(四)InputStreamReader和OutputStreamWriter:https://blog.csdn.net/qq_41061437/article/details/81745300
Java中io流的学习(五)FileReader和FileWriter:https://blog.csdn.net/qq_41061437/article/details/81747105
Java中io流的学习(六)BufferedReader和BufferedWriter:https://blog.csdn.net/qq_41061437/article/details/81747323
Java中io流的学习(七)ObjectInputStream和ObjectOutputStream:https://blog.csdn.net/qq_41061437/article/details/81748461
Java中io流的学习(八)PrintStream和PrintWriter:https://blog.csdn.net/qq_41061437/article/details/81782770
Java中io流的学习(九)RandomAccessFile:https://blog.csdn.net/qq_41061437/article/details/81805351
Java中io流的学习(十)ByteArrayInoutStream和ByteArrayOutputStream:https://blog.csdn.net/qq_41061437/article/details/81806245
Java中io流的学习(十一)NIO:https://blog.csdn.net/qq_41061437/article/details/81809370
Java中io流的学习(总结):https://blog.csdn.net/qq_41061437/article/details/81740680