java学习:字符缓冲流
1.字符缓冲流是什么?
FileReader sc=new FileReader(“D:\javac\projects\untitled10\src\TRE.txt”)
1.读取文件内容
BufferedReader br=new BufferedReader(new FileReader(“D:\javac\projects\untitled10\src\TRE.txt”));
读取数据
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class tred {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("D:\\javac\\projects\\untitled10\\src\\TRE.txt"));
int ch;
while((ch=br.read())!=-1){
System.out.println((char)ch);
}
}
}
结果:
2.输入数据:
BufferedWriter sc=new BufferedWriter(new FileWriter(“D:\javac\projects\untitled10\src\TRE.txt”));
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class erty {
public static void main(String[] args) throws IOException {
BufferedWriter sc=new BufferedWriter(new FileWriter("D:\\javac\\projects\\untitled10\\src\\TRE.txt"));//输入数据
sc.write("hello\r\n"); //\n: 分行了
sc.write("world");
sc.close();
}
}
结果:
3.字符缓冲流特有功能:
读一行文字:
f1.readLine();// 字符串;
如果:结尾是:空的,则结束