printWriter: 打印输出流
构造函数:
1.字符串路径
2.File对象
3.字节输出流
4.字符输出流
例子:
直接通过控制台输入…写入到文件中
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
//增强效率的流
BufferedReader bufr = new BufferedReader(new InputStremReader(System.in));
//需要写入的位置
printWriter piw = new printWriter("out.txt");
String srt = null;
wile((str = piw.readLine()) != null) {
if("over".equals(str)) {
break;
}
piw.println(str);
piw.flush();//刷新
}
bufr.close();
piw.close();
}
}
写入out.txt
文本文件
打开out.txt文件
如果在printWriter piw = new printWriter("out.txt")
加个true;
例如: printWriter piw = new printWriter(“out.txt”, true);
这样就不用flush();进行刷新了.