打印流

在整个IO包中,打印流是输出信息最方便的类。

 

PrintStream(字节打印流)和PrintWriter(字符打印流)

提供了一系列重载的print和println方法,用于多种数据类型的输出

PrintStream和PrintWriter的输出不会抛出异常

PrintStream和PrintWriter有自动flush功能

System.out返回的是PrintStream的实例

package com.xatu.IO;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class printWriter {

	public static void main(String[] args) {
		Writer fw = null;
		BufferedWriter bufw = null;
		PrintWriter pw = null;
		try {

			String[] text = {
					"zheshiyaoxiedewenjian\n",
					"zheshiyaoxiedewenjian\n",
					"zheshiyaoxiedewenjian\n",
					"zheshiyaoxiedewenjian\n",
					"zheshiyaoxiedewenjian\n",
					"zheshiyaoxiedewenjian\n",
					"zheshiyaoxiedewenjian\n"
			};
			fw = new FileWriter("wenbwnwenjian2",true);
			bufw = new BufferedWriter(fw);
			pw = new PrintWriter(bufw);
			for (String str : text) {
				pw.print(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (pw != null)
				try {
					pw.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}

}

 

你可能感兴趣的:(java,JavaSE——IO)