Thinking in java 琐碎知识点之 I/O流 、对象序列化(六)

2014-11-23 21:43:47 · 作者: · 浏览: 33
; //为了达到最高效率,可要考虑在 BufferedReader内包装 InputStreamReader BufferedReader bfr = new BufferedReader(isr); String line = null; while ((line = bfr.readLine()) != null) { // 使用了while循环,则要自定义结束循环的语句 if ("exit".equals(line)) { System.out.println("程序终止!"); break; } System.out.println(line); } } } 例程5: import java.io.*; public class KeyInDemo2 { public static void main(String[] args) throws IOException { InputStream in = System.in;// 返回字节输入流对象 //将字节输入流转换成字符输入流 InputStreamReader isr = new InputStreamReader(in); // 为了达到最高效率,可要考虑在 BufferedReader内包装 InputStreamReader BufferedReader bfr = new BufferedReader(isr); OutputStream out=new FileOutputStream("F:\\test.txt"); //将字节输出流转化为字符输出流 OutputStreamWriter osw=new OutputStreamWriter(out); BufferedWriter bfw=new BufferedWriter(osw); String line = null; while ((line = bfr.readLine()) != null) { // 使用了while循环,则要自定义结束循环的语句 if ("exit".equals(line)) { System.out.println("程序终止!"); break; } //一行一行的写 bfw.write(line); bfw.newLine(); bfw.flush(); } } }
9. 处理流PrintStream流的用法
import java.io.*;
/*
 * PrintStream类的输出功能非常强大,通常需要输出文本内容时,
 * 应该将输出流包装成PrintStream后再输出
 * 对应的由于BufferedReader具有readLine方法,可以方便地一次读取一行内容,
 * 所以经常把读取文本的输入流包装成BufferedReader,用以方便读取输入流的文本内容
 */
public class PrintStreamTest {
	public static void main(String[] args) {
		PrintStream pst=null;//称作处理流或包装流
		try {
			FileOutputStream fos=new FileOutputStream("F:\\Thinking.txt");//直接和物理文件打交道的流也称节点流
			//以PrintStream来包装FileOutputStream输出流
			pst=new PrintStream(fos);
			//使用printStream来实现输出
			pst.println("普通字\r\n符串");
			pst.println(new PrintStreamTest());
			
		} catch (FileNotFoundException e) {
			e.printStackTrace(pst);
		}
		finally{
			if(pst!=null){
			pst.close();}
		}
	}
}

10、重定向标准输入输出流
Java的标准输入输出分别通过System.in和System.out来代表,默认情况下它们分别代表键盘和显示器
System类提供了重定向标准输入输出的方法
例程:RedirectIn.java
import java.io.*;
import java.util.Scanner;
public class RedirectIn {
	public static void main(String[] args) {
		FileInputStream fis = null;
		try {
			// 将标准输入重定向到fis输入流
			fis = new FileInputStream("F:\\Thinking.txt");
			System.setIn(fis);
			//使用流接受标准输入
//			BufferedReader bf = new BufferedReader(new InputStreamReader(
//					System.in));
//			String len = "";
//			while ((len = bf.readLine()) != null) {
//				System.out.println(len);
//			}
			
			//使用Scanner
			Scanner sn=new Scanner(System.in);
			System.out.println("=====使用Scanner=====");
			while(sn.hasNext()){
				System.out.println(sn.next());
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


}


例程:RedirectOut.java
import java.io.*;
public class RedirectOut {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PrintStream p=null;
		try {
			p=new PrintStream(new FileOutputStream("F:\\out.txt"));
			p.println("p.println()");
			System.out.println("重定向输出流到F:\\out.txt");
			System.setOut(p);
			System.out.println("此行不显示在屏幕上(不在控制台输出),直接写入了文件");