JAVA中的IO流-printWriter(打印输出流)

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();进行刷新了.

发布了87 篇原创文章 · 获赞 43 · 访问量 3993

猜你喜欢

转载自blog.csdn.net/weixin_42947972/article/details/103588876