关于java:字符缓冲流案例复制Java文件字符缓冲流

30次阅读

共计 703 个字符,预计需要花费 2 分钟才能阅读完成。

一、字符缓冲流

public class BufferedReader extends Reader
public class BufferedWriter extends Writer

构造函数:

BufferedReader(Reader in)
BufferedWriter(Writer out)


写数据:
FileWriter fw = new FileWriter("sdfsf\\sfs.java");
BufferedWriter bw = new BufferedWriter(bw);
两行简化为一行:BufferedWriter bw = new BufferedWriter(new FileWriter("sdfsf\\sfs.java"));
bw.write("hello\r\n");
bw.writer("world\r\n");
读数据:一次读一个字符数据
BufferedReader br = new BufferedReader(new FileReader("sdfsf\\sfs.java"));
int ch;
while((ch = br.read())!=-1){sout((char)ch);
}
br.close();
读数据:一次读一个字符数组数据
BufferedReader br = new BufferedReader(new FileReader("sdfsf\\sfs.java"));
char[] chs = new char[1024];
int len;
while((len = br.read(chs))!=-1){sout(new String(chs,0,len));
}
br.close();

二、案例:


操作:
一次读写一个字符数据

一次读写一个字符数组数据

正文完
 0