`
imaginecup
  • 浏览: 85478 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

java编程思想 IO7 文件操作源码

阅读更多
希望大家留言一起讨论讨论。

package com.dirlist;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Echo {
package com.dirlist;
import java.io.*;
import java.util.Scanner;
public class Redirecting {

	/**
	 * 标准I/O重定向
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//存储System.in和System.out最初的引用
		PrintStream console=System.out;
		InputStream inconsole=System.in;
		BufferedInputStream in=new BufferedInputStream(new FileInputStream("src\\com\\dirlist\\Redirecting.java"));
		PrintStream out=new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
		/*
		 * 重新分配“标准”输入流。 首先,如果有安全管理器,则通过 RuntimePermission("setIO") 
		 * 权限调用其 checkPermission 方法,查看是否可以重新分配“标准”输入流。 
		 */
		//将控制台输入重定向到文件输入
		System.setIn(in);
		/*
		 * 重新分配“标准”输出流。 首先,如果有安全管理器,则通过 RuntimePermission("setIO") 
		 * 权限调用其 checkPermission 方法,查看是否可以重新分配“标准”输入流。 
		 */
		//将控制台输出重定向到文件输出
		System.setOut(out);
		//重新分配“标准”错误输出流。
		System.setErr(out);
		
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s;
		while((s=br.readLine())!=null){
			System.out.println(s);
		}
		out.close();
		//恢复对控制台的重定向
		System.setOut(console);
		System.setIn(inconsole);
		
		System.out.println("重定向好难啊!");
		Scanner sc=new Scanner(System.in);
		System.out.println(sc.next());
		//这个程序将标准输入连接到文件上,并将标准输出重定向到另一个文件上。
		//注意它在程序开头处存储了对最初System.in和System.out的引用,并且在结尾处恢复到该对象上.
	}

}
  /** * 从标准输入中读取 * @throws IOException */ public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String s; //Any empty line or Ctr-Z terminates the program while((s=stdin.readLine())!=null&&s.length()!=0){ System.out.println(s.toUpperCase()); } } } package com.dirlist; import java.io.PrintWriter; public class ChangeSystemOut { /** * 将System.out转换成PrintWriter */ public static void main(String[] args) { PrintWriter pw=new PrintWriter(System.out,true); pw.println("Hello World!"); } }

 

package com.dirlist;
import java.io.*;
import java.util.Scanner;
public class Redirecting {

	/**
	 * 标准I/O重定向
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//存储System.in和System.out最初的引用
		PrintStream console=System.out;
		InputStream inconsole=System.in;
		BufferedInputStream in=new BufferedInputStream(new FileInputStream("src\\com\\dirlist\\Redirecting.java"));
		PrintStream out=new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
		/*
		 * 重新分配“标准”输入流。 首先,如果有安全管理器,则通过 RuntimePermission("setIO") 
		 * 权限调用其 checkPermission 方法,查看是否可以重新分配“标准”输入流。 
		 */
		//将控制台输入重定向到文件输入
		System.setIn(in);
		/*
		 * 重新分配“标准”输出流。 首先,如果有安全管理器,则通过 RuntimePermission("setIO") 
		 * 权限调用其 checkPermission 方法,查看是否可以重新分配“标准”输入流。 
		 */
		//将控制台输出重定向到文件输出
		System.setOut(out);
		//重新分配“标准”错误输出流。
		System.setErr(out);
		
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s;
		while((s=br.readLine())!=null){
			System.out.println(s);
		}
		out.close();
		//恢复对控制台的重定向
		System.setOut(console);
		System.setIn(inconsole);
		
		System.out.println("重定向好难啊!");
		Scanner sc=new Scanner(System.in);
		System.out.println(sc.next());
		//这个程序将标准输入连接到文件上,并将标准输出重定向到另一个文件上。
		//注意它在程序开头处存储了对最初System.in和System.out的引用,并且在结尾处恢复到该对象上.
	}

}

IO重定向操纵的是字节流,不是字符流。 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics