Java 中的 print 流:

  print 流用于做输出将会非常的方便,并且具有以下特点:

  1. printWriter、printStream 都属于输出流,分别针对字符,字节.

  2. printWriter、printStream 提供了重载的 print 与 println 方法.

  3.  print 与 println 能够用于多种数据类型的输出.

  4.  printWriter、printStream 的输出操作不会抛出异常,用户通过检测错误状态获取错误信息.

  5. printWriter、printStream 都有自动 flush() 的功能.

  6. print 流具有以下常用方法:

    printWriter(Writer out)

    printWriter(Writer out, boolean autoFlush)

    printWriter(outputStream out)

          printWriter(outputStream out,  boolean autoFlush)

    printStream(outputStream out)

    printStream(outputStream out,  boolean autoFlush)

Demo_1:

import java.io.*;
class Test {
	public static void main(String[] args) {
		try {
			FileOutputStream fos = new FileOutputStream("E:/eclipse projiect/TEST0503.txt");
			PrintStream ps = new PrintStream(fos);
			if(ps!=null){
				System.setOut(ps); // System.out 默认的是在命令行中输出
			}                      // System.setOut 重新设置out的值
			int ln = 0;            // System.out 就是 printStream 类型
			for(char c=0;c<=6000;c++){
				System.out.println(c+" ");
				ln++;
				if(ln>=100){
					System.out.println();
				}
				ln = 0;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}

 配图:

Demo_2:

import java.io.*;
class Test {
	public static void list(String fileName, PrintStream ps) {
		try {
			FileReader fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);
			String s = null;
			while((s=br.readLine())!=null){
				ps.print(s);
				System.out.println();
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e){
			ps.println("无法读取文件");
		} 
	}
	public static void main(String[] args) {
		String fileN = "E:/eclipse projiect/TEST.txt";
		if(fileN != null) {
			list(fileN, System.out);
		}
	}
}

 运行结果:

ni hao: 中国0123
江城如画里,
山晚望晴空。
两水夹明镜,
双桥落彩虹。
人烟寒橘柚,
秋色老梧桐。
谁念北楼上,
临风怀谢公。

Demo_3:

import java.io.*;
import java.util.Date;
class Test {
	public static void main(String[] args) {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		try {
			FileWriter fw = new FileWriter("E:/eclipse projiect/TEST0504.txt", true);
			PrintWriter log = new PrintWriter(fw);
			String s = null;
			while((s=br.readLine())!=null){
				if(s.equalsIgnoreCase("exit")){
					break;
				}
				System.out.println(s.toUpperCase());
				log.println("-----");
				log.println(s.toUpperCase());
				log.println("&&&&&"+ new Date()+"&&&&&");
				log.flush(); // log 自带flush功能,但是写了比较好
			}
			log.println("&&&&&"+ new Date()+"&&&&&");
			log.flush();
			log.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 运行结果:

Eclipse 中的 console 中的内容如下:

wo men zhou mo qu na wan?
WO MEN ZHOU MO QU NA WAN?
qu chong qing ba ?
QU CHONG QING BA ?
hao ya
HAO YA

TEST0504.txt 文件中的内容如下:

-----
WO MEN ZHOU MO QU NA WAN?
&&&&&Wed May 03 15:18:30 CST 2017&&&&&
-----
QU CHONG QING BA ?
&&&&&Wed May 03 15:18:38 CST 2017&&&&&
-----
HAO YA
&&&&&Wed May 03 15:18:40 CST 2017&&&&&

posted on 2017-05-03 12:22  牧羊人的世界  阅读(468)  评论(0编辑  收藏  举报