字节流不能直接操作字符,因为一个字符有两个字节,而字节流一次只能操作一个字节.所以便有了字符流的存在。相对于字节流,字符流快捷且空间大。
输出字符流:把要写入文件的字符序列(实际是unicode码元序列)转为指定编码方式下的字节序列,然后在写入文件中。
输入字符流:把要读取的字节序列按照指定编码方式转为相应的字符序列(实际是unicode码元序列),从而写入内存中。
@Test
public void test1() throws Exception {
//字节流不能写字符流 浪费空间
FileInputStream f = new FileInputStream("C:/Users/流/aa.txt");
int a;
while((a = f.read())!=-1){
System.out.println((char)a);
}
f.close();
}
@Test
public void test2() throws Exception {
FileWriter w = new FileWriter("C:/Users/流/aa.txt");
// w.write('A');
// w.write('哈'); // 写入字符
w.write("AASFDDVV哈哈那时候后发给你吧"); // 写入字符串
w.close();
}
@Test
public void test3() throws Exception {
FileRea