字符流只能操作文字,而字节流可以操作任何文件。
用于写入字符流的抽象类。 子类必须实现的唯一方法是write(char [],int,int),flush()和close()。 但是,大多数子类将覆盖此处定义的一些方法,以提供更高的效率,附加功能或两者兼而有之。
其子类FileWriter
使用默认缓冲区大小将文本写入字符文件,FileWriter用于编写字符流.
public class Writer {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d://b.txt");
//在D盘创建了b.txt文件,并将a写了进去。
fw.write('a');
fw.close();
}
}
append(char c)方法
将指定的字符追加到此writer。
public class Writer {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d://b.txt");
fw.append("锄禾日当午").append(",").append("汗滴禾下土");
fw.close();
}
}
字符输入流Reader
其子类FileReader:`FileReader`用于读取字符流
public class Writer {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("d://b.txt");
//读取文件中的所有内容
char[] chars = new char[100];
//文件中所有内容的长度
int len = fr.read(chars);
//从文件内容的第一个到最后一个 都读出
String text = new String(chars,0,len);
System.out.println(text);
System.out.println(text.length());
fr.close();
}
}