构造方法:BufferedReader br = new BufferReader(Reader in);
主要方法:int read();//读取单个字符。
int read(char[] cbuf,int off,int len);//将字符读入到数组的某一部分。返回读取的字符数。达到尾部 ,返回-1。
String readLine(); //读取一个文本行。
void close(); //关闭该流。并释放与该流相关的所有资源。
- package Buffered;
- import ;
- import ;
- import ;
- public class BufferedWriterDemo {
- public static void main(String[] args) throws IOException {
- FileWriter fw = new FileWriter("");
- // ("ok168");
- // ();
- /**
- * 为了提高写入的效率,使用了字符流的缓冲区。
- * 创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联。
- */
- BufferedWriter bufw = new BufferedWriter(fw);
- //使用缓冲区中的方法将数据写入到缓冲区中。
- ("hello world !");
- ();
- ();
- ("!hello world !");
- ("!hello world !");
- //使用缓冲区中的方法,将数据刷新到目的地文件中去。
- ();
- //关闭缓冲区,同时关闭了fw流对象
- ();
- }
- }
package Buffered;
import ;
import ;
import ;
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("");
// ("ok168");
// ();
/**
* 为了提高写入的效率,使用了字符流的缓冲区。
* 创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联。
*/
BufferedWriter bufw = new BufferedWriter(fw);
//使用缓冲区中的方法将数据写入到缓冲区中。
("hello world !");
();
();
("!hello world !");
("!hello world !");
//使用缓冲区中的方法,将数据刷新到目的地文件中去。
();
//关闭缓冲区,同时关闭了fw流对象
();
}
}
自定义的一个myBufferedReader类。
- package Buffered;
- import ;
- import ;
- public class MyBufferedReader {
- private FileReader fr;
- private char []buf = new char[1024];
- private int count = 0;
- private int pos = 0;
- public MyBufferedReader(FileReader f){
- this.fr = f;
- }
- public int myRead() throws IOException{
- if(count == 0){
- count = (buf);
- pos = 0;
- }
- if(count<0)
- return -1;
- int ch = buf[pos++];
- count--;
- return ch;
- }
- public String myReadLine() throws IOException{
- StringBuilder sb = new StringBuilder();
- int ch = 0;
- while ((ch = myRead()) != -1) {
- if (ch == '\r')
- continue;
- if (ch == '\n')
- return ();
- ((char) ch);
- if(count == 0)
- return ();
- }
- return null;
- }
- public void myClose() throws IOException {
- ();
- }
- }
package Buffered;
import ;
import ;
public class MyBufferedReader {
private FileReader fr;
private char []buf = new char[1024];
private int count = 0;
private int pos = 0;
public MyBufferedReader(FileReader f){
= f;
}
public int myRead() throws IOException{
if(count == 0){
count = (buf);
pos = 0;
}
if(count<0)
return -1;
int ch = buf[pos++];
count--;
return ch;
}
public String myReadLine() throws IOException{
StringBuilder sb = new StringBuilder();
int ch = 0;
while ((ch = myRead()) != -1) {
if (ch == '\r')
continue;
if (ch == '\n')
return ();
((char) ch);
if(count == 0)
return ();
}
return null;
}
public void myClose() throws IOException {
();
}
}
使用bufferedReader 和bufferWriter方法写的一个复制文本的小程序。
- package IOtest;
- import ;
- import ;
- import ;
- import ;
- import ;
- public class TextCopyByBuf {
- /**
- * 首先创建读取字符数据流对象关联所要复制的文件。
- * 创建缓冲区对象关联流对象。
- * 从缓冲区中将字符创建并写入到要目的文件中。
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- FileReader fr = new FileReader("C:\\");
- FileWriter fw = new FileWriter("D:\\");
- BufferedReader bufr = new BufferedReader(fr);
- BufferedWriter bufw = new BufferedWriter(fw);
- //一行一行的寫。
- String line = null;
- while((line = ()) != null){
- (line);
- ();
- ();
- }
- /* 一個字節一個字節的寫。
- int ch = 0;
- while((ch = ())!=-1){
- (ch);
- }*/
- ();
- ();
- }
- }