2016-07-24
FileReader
1,FileWriter
课下作业: c: 定义两个文件 in.txt,out.txt
然后,in.txt里面写入部分中文以及英文。
然后写程序读取in.txt的内容,写到out.txt里面。
规定:读文件的类必须是FileReader
写文件的类必须是FileWriter。
package com.java1995; import java.io.FileWriter; import java.io.IOException; public class TestFileWriter { public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter("D:\workspace\java_io\out.txt"); String str = "这个内容是中文的,需要向文件里写,adsgsgf23553265"; for (int i = 0; i < str.length(); i++) { fw.write(str.charAt(i)); } fw.flush();// 最好是手动调用 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭流要严谨 if (fw != null) { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
【参考资料】