FileWriter文件字符输出流,写
- 只能输出普通文本
- FileReader 文件字符输入流,只能读取普通文本
- 读取文本内容时比较方便快捷
public class FileReaderTest {
public static void main(String[] args) {
FileReader reader =null;
//创建文件字符输入流
try {
reader =new FileReader("fr");
//读
char[] chars =new char[4];
/**
* 采用while读
*/
int readCount =0;
while ((readCount=reader.read(chars))!=-1){
System.out.println(new String(chars,0,readCount));
}
/**
* 采用for读
*/
reader.read(chars);
for (char c:chars){
System.out.println(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class FileWriterTest {
public static void main(String[] args) {
FileWriter out =null;
try {
out =new FileWriter("fw");
//刷新
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}