IO流之FileWriter(文件字符输出流)和FileReader(文件字符输入流)
import java.io.*;
public class FileCharacterinputAndoutput {
public static void main(String[] args) {
File f = new File("D:/word.txt");
FileWriter fw = null;
try {
fw = new FileWriter(f,false);
String str = "天行健,君子以自强不息。";
fw.write(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileReader fr = null;
try {
fr = new FileReader(f);
char c[] = new char[1024];
int count;
while((count = fr.read(c)) != -1) {
System.out.println("文件中的数据是:" + new String(c,0,count));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}