黑马程序员23-4: PrintStream打印流,从控制台输入,输出到文件中


------- android培训 java培训、期待与您交流!-------



package cn.itcast.io.p4.print.demo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

public class PrintStreamDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		/*
		 * PrintStream:
		 * 1,提供了打印方法可以对多种数据类型值进行打印。并保持数据的表示形式。 
		 * 2,它不抛IOException.
		 * 
		 * 构造函数,接收三种类型的值:
		 * 1,字符串路径。
		 * 2,File对象。
		 * 3,字节输出流。
		 */
		
		PrintStream out = new PrintStream("print.txt");
		
//		int by = read();
//		write(by);
		
//		out.write(610);//只写最低8位,
		
//		out.print(97);//将97先变成字符保持原样将数据打印到目的地。 
		
		out.close();
		
		
	}

}


package cn.itcast.io.p4.print.demo;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class PrintWriterDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		/*
		 * PrintWriter:字符打印流。
		 * 构造函数参数:
		 * 1,字符串路径。
		 * 2,File对象。
		 * 3,字节输出流。
		 * 4,字符输出流。
		 * 
		 */
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
		
		PrintWriter out = new PrintWriter(new FileWriter("out.txt"),true);
		
		String line =  null;
		while((line=bufr.readLine())!=null){
			if("over".equals(line))
				break;
			out.println(line.toUpperCase());
//			out.flush();
		}
		
		out.close();
		bufr.close();
	}

}


------- android培训 java培训、期待与您交流!-------


详细请查看: http://edu.csdn.net/heima -------


你可能感兴趣的:(Stream)