java父子进程管道通信示例

包含两个java文件,直接运行主进程的java文件就可以了,主进程会自动启动子进程

主进程代码如下:

 

package com.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * Date: 12-1-13
 * Time: 10:52:00
 */
public class Main {


	public static BufferedReader inp;
	public static BufferedWriter out;

	public static void print(String s) {
	//	logger.info(s);
		System.out.println(s);
	}

	public static String pipe(String msg) {
		String ret;
		try {
			out.write(msg + "\n");
			out.flush();
			ret = inp.readLine();
			return ret;
		} catch (Exception err) {

		}
		return "";
	}


	public static void main(String[] args) {
		String cmd = "\"D:\\Program Files\\Java\\jdk1.6.0_16\\bin\\java\" -Didea.launcher.port=7532 \"-Didea.launcher.bin.path=D:\\Program Files\\JetBrains\\IntelliJ IDEA 10.5\\bin\" -Dfile.encoding=GBK -classpath \"D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\alt-rt.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\charsets.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\deploy.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\javaws.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\jce.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\jsse.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\management-agent.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\plugin.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\resources.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\rt.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\ext\\dnsns.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\ext\\localedata.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\ext\\sunjce_provider.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\ext\\sunmscapi.jar;D:\\Program Files\\Java\\jdk1.6.0_16\\jre\\lib\\ext\\sunpkcs11.jar;E:\\projects\\JProj_Idea\\PipeTest\\out\\production\\PipeTest;D:\\Program Files\\JetBrains\\IntelliJ IDEA 10.5\\lib\\idea_rt.jar\" com.intellij.rt.execution.application.AppMain SubClass";
		try {

//			print(cmd);
//			print(System.getProperty("user.dir"));
			Process p = Runtime.getRuntime().exec(cmd);
			inp = new BufferedReader(new InputStreamReader(p.getInputStream()));
			out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
			print(pipe("你好"));
			print(pipe("RoteM"));

			pipe("quit");
			inp.close();
			out.close();
		} catch (Exception err) {
			err.printStackTrace();
		}
	}
}

 

子进程代码如下:

 

import java.io.*;

/**
 * Date: 12-1-13
 * Time: 10:54:00
 */
public class SubClass {
	static BufferedReader bufferedReader;
	static BufferedWriter bufferedWriter;

	static {
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(System.in));
			File f = new File("e:\\111.txt");
			bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "GBK"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static String readFormParentProcess() throws Exception {
		String result = bufferedReader.readLine();
		return result;
	}

	public static void main(String[] args) throws Exception {
		File f = new File("e:\\111.txt");
		String s = readFormParentProcess();
		bufferedWriter.write(s);
		System.out.println("sub class");
		bufferedWriter.flush();
		bufferedWriter.close();
		bufferedReader.close();
	}
}

你可能感兴趣的:(java)