java I/O进程控制,重定向 示例代码

31 篇文章 0 订阅

java I/O进程控制,重定向 示例代码

package org.rui.io.util;

import java.io.*;
/**
 * 标准I/O重定向 
 */
public class Redircting {
	public static void main(String[] args) throws IOException {
		String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\util/";
		PrintStream console=System.out;
		//输入
		BufferedInputStream in=new BufferedInputStream(
				new FileInputStream(path+"Redircting.java")
				);
		//输出
		PrintStream out=new PrintStream(
				new BufferedOutputStream(
						new FileOutputStream(path+"testf.out")
						)
				);
		//允许我们对标准输入输出  和错误IO流进行重定向
		System.setIn(in);
		//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();// remember this!
		System.setOut(console);;
	}
	
}
/**
 * output:
 * 同上源码
 */

package org.rui.io.util;

public class OSExecuteException  extends RuntimeException{
	public OSExecuteException(String why){super(why);}

}

package org.rui.io.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
 * 进程控制
 * 
 * 你经常会需要在java内部执行其它操作系统程序,并且要控制这此程序的输入和输出,java类库提供了执行这些操作的类
 * 
 * 为了捕获程序执行时产生的标准输出流,你需要调用getInputStream(),这是因为。。。是我们可以从中读取信息的流。
 * 从程序 中产生的结果每次输出一行,因此要使用readLine
 *
 */
public class OSExecuted {

	public static void command(String command)
	{
		boolean err=false;
		try {
			/**此类用于创建操作系统进程。
			 * 每个 ProcessBuilder 实例管理一个进程属性集。start() 方法利用这些属性创建一个新的 Process 实例。
			 * start() 方法可以从同一实例重复调用,以利用相同的或相关的属性创建新的子进程。
			 */
			Process process=new ProcessBuilder(command.split(" ")).start();
			//这里还可以返回它们,暂只打印
			BufferedReader br=new BufferedReader(
					new InputStreamReader(
							process.getInputStream()
							));
			String s;
			while((s=br.readLine())!=null)
				System.out.println(s);
			
			//捕获错误流
			BufferedReader b2=new BufferedReader(
					new InputStreamReader(
							process.getErrorStream()
							));
			while((s=b2.readLine())!=null)
			{
				System.err.println("sssssssss:"+s);
				err=true;
			}
				
		} catch (Exception e) {
			System.out.println("command Ex =="+command);
			if(command.startsWith("CMD /C"))
				command("CMD /C"+command);
			else
				throw new RuntimeException(e);
		}
		
		if(err)
			throw new OSExecuteException(" Errors execting "+ command);
	}

}

package org.rui.io.util;
/**
 * 下面展示了如何使用OSExecute的示例
 * @author lenovo
 *
 */
public class OSExecuteDemo {
	public static void main(String[] args) {
		//javac org/rui/io/util/OSExecuteDemo.java
		String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\util/";
		//path="";
		String commandw="javap "+path+"OSExecuteDemo.class";
		//System.out.println(commandw);
		OSExecuted.command(commandw);
	}

}
/************************output:
Compiled from "OSExecuteDemo.java"
public class org.rui.io.util.OSExecuteDemo {
// public org.rui.io.util.OSExecuteDemo();
//public static void main(java.lang.String[]);
}
 *******/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值