1、

/**
* 输入字符流:
* --------|Reader 所有输入字符流的基类。抽象类
* -----------|FileReader 读取文件字符串的输入字符流。
* -----------|BufferedReader 缓冲输入字符流。目的是:提高读取效率和拓展FileReader的功能。(该类内部也是维护了字节数组)
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\a.txt");
//建立通道
FileReader fileReader =new FileReader(file);
//建立缓冲输入字符流
BufferedReader bufferedReader = new BufferedReader(fileReader);
//读取数据
int content = 0;
while ((content = bufferedReader.read())!=-1) {
System.out.print((char)content);
}
bufferedReader.close();
}
}

2、一次读一行

public class Demo2 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\a.txt");
//建立通道
FileReader fileReader =new FileReader(file);
//建立缓冲输入字符流
BufferedReader bufferedReader = new BufferedReader(fileReader);
//读取数据
String content=null;
//一次读一行,注意返回的数据
while ((content = bufferedReader.readLine())!=null) {
System.out.println(content);
}
bufferedReader.close();
}
}

3、bufferedWriter

public class Demo2 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\b.txt");
FileWriter fileWriter = new FileWriter(file,true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//String string="努力编程";
String string="哈哈";
bufferedWriter.newLine();//相当于换行!!
bufferedWriter.write(string);
bufferedWriter.close();
}
}
05-04 05:46