FileReader(字符输入流):
- 文件字符输入流,只能读取普通文本。
- 读取文本内容时,比较方便,快捷。
示例代码01:
public class FileReaderTest {
public static void main(String[] args) {
FileReader reader = null;
try {
reader = new FileReader("IO/src/com/newstudy/javase/io/temp4.txt");
char[] chars = new char[4];
reader.read(chars);
for(char c : chars) {
System.out.print(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/e2e54db12a3b4fa8955d3cc6fa5ae1b1.png)
运行结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/a8313962f4ef44c2ad2bb1aa7e846556.png)
FileWriter(字符输出流):
示例代码02:
public class FileWriterTest {
public static void main(String[] args) {
FileWriter out = null;
try {
out = new FileWriter("file",true);
char[] chars = {'我','是','中','国','人'};
out.write(chars);
out.write(chars,2,3);
out.write("我是一名java软件工程师!");
out.write("\n");
out.write("HelloWorld!");
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/dec42b152a034fdbacf82e6418cb5ff0.png)