/**
* ClassName: CharStreamDemo
* 字符流:
* 字符输出流:Writer,对文件的操作 使用子类:FileWriter
* 字符输入流:Reader,对文件的操作使用子类:FileReader
* 每次操作的单位是一个字符
* 内部实现还是字节流
* 文件字符操作流会自带缓存,默认大小为1024字节,在缓存满后,或手动刷新缓存,或关闭流时会把数据写入文件
*
* 操作非文本文件时使用字节流,操作文本文件时使用字符流方便一些
* @author cai
* @date 2018年10月16日
*/
public class CharStreamDemo {
private static void out() {
File file = new File("C:\\Users\\Desktop\\需要被删的\\ffff\\新建文本文档.txt");
try {
Writer out = new FileWriter(file,true);
out.write("\\n明天上证指数100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void in() {
File file1 = new File("C:\\Users\\Desktop\\需要被删的\\ffff\\新建文本文档.txt");
try {
Reader in = new FileReader(file1);
char[] cs = new char[1];
int len = -1;
StringBuilder buf= new StringBuilder();
while((len = in.read(cs))!=-1) {
buf.append(new String(cs,0,len));
}
in.close();
System.out.println(buf);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
out();
in();
}
}