Reader/Writer
//能够用文本编辑器打开的文件,不乱码就是字符文件,字符————16
//用文本编辑器打开乱码的,就是字节文件,字节————8
(用字符流读取/写文本文件)
public class TestChar {
public static void main(String[]args) throws Exception{
// read();
write();
}
private static void write() throws Exception{
Writer w=new FileWriter("oop/a.txt");
char [] cs= {'啊','加','西'};
w.write(cs);
w.close();
}
// private static void read() throws Exception{
// Reader r=new FileReader("oop/a.txt");
// int a=r.read();
// System.out.println((char)a);
// char[] chars=new char[1024];
// int length=r.read(chars);
// System.out.println(Arrays.toString(chars));
// r.close();
// }
}
(用BufferedReader读取文件,用BufferedWriter写文件,可以提高文件读取和写文件的效率)
public class TestBuffer {
public static void main(String[]args) throws Exception{
// buReader();
buWrite();
}
private static void buWrite() throws Exception {
BufferedWriter bw=new BufferedWriter(new FileWriter("oop/a.txt"));
bw.write("王晋是250");
//刷新缓存 //bw.flash();
bw.close();//默认执行flash,关闭管道
}
private static void buReader() throws Exception{
BufferedReader br=new BufferedReader(new FileReader("oop/a.txt"));
// String str=br.readLine();
// str=br.readLine();
// System.out.println(str);
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
}
}