import java.util.*;
import java.io.*;
/**
@version 1.0
*/
public class WriteToProcess
{
public static void main(String[] args)
{
PrintStream ps = null;
try
{
//运行java ReadStandard命令,返回运行该命令的子进程
Process p = Runtime.getRuntime().exec("java ReadStandard");
//以p进程的输出流创建PrintStream对象
//这个输出流对本程序是输出流,对p进程则是输入流)
ps = new PrintStream(p.getOutputStream());
//向ReadStandard程序写入内容,这些内容将被ReadStandard读取
ps.println("普通字符串");
ps.println(new WriteToProcess());
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (ps != null)
ps.close();
}
}
}
//定义一个ReadStandard类,该类可以接受标准输入,
//并将标准输入写入out.txt文件。
class ReadStandard
{
public static void main(String[] args) throws Exception
{
//使用System.in创建Scanner对象,用于获取标准输入
Scanner sc = new Scanner(System.in);
PrintStream ps = new PrintStream(
new FileOutputStream("out.txt"));
//增加下面一行将只把回车作为分隔符
sc.useDelimiter("\n");
//判断是否还有下一个输入项
while(sc.hasNext())
{
//输出输入项
ps.println("键盘输入的内容是:" + sc.next());
}
ps.close();
}
}
本程序在cmd窗口下正常运,可以生成out.txt,其中的内容为
键盘输入的内容是:普通字符串
键盘输入的内容是:WriteToProcess@1b67f74
然而在Eclipse中运行却无法生成out.txt???
使用Runtime对象的exec()方法可以获得其他进程的Process对象,Process对象代表由该Java程序启动的子进程,Process类提供了如下3个方法,用于让程序和其子进程进行通讯。
InputStream getErrorStream():获取子进程的错误流
InputStream getInputStream():获取子进程的输入流
OutputStream getOu......
答案就在这里:Java虚拟机读写其他进程的数据
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。
在Eclipse下执行时,本质上你的java ReadStandard进程调用是异常了的。
进程错误信息为:错误: 找不到或无法加载主类 ReadStandard/n
修正代码,输出进程的异常信息,就是上述异常信息。因为Eclipse是已经封装了的java命令,可以直接右键执行,所以无法识别java命令。而在cmd环境下是能够正确识别java命令正确执行的。
public class WriteToProcess {
public static void main(String[] args) {
PrintStream ps = null;
try {
// 运行java ReadStandard命令,返回运行该命令的子进程
System.out.println("start");
Process p = Runtime.getRuntime().exec("java ReadStandard");
// 以p进程的输出流创建PrintStream对象
// 这个输出流对本程序是输出流,对p进程则是输入流)
ps = new PrintStream(p.getOutputStream());
// 向ReadStandard程序写入内容,这些内容将被ReadStandard读取
ps.println("普通字符串");
ps.println(new WriteToProcess());
InputStream error = p.getErrorStream();
System.out.println(convertStreamToString(error));
System.out.println("start");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (ps != null)
ps.close();
}
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
// 定义一个ReadStandard类,该类可以接受标准输入,
// 并将标准输入写入out.txt文件。
class ReadStandard {
public static void main(String[] args) throws Exception {
System.out.println("ReadStandard.");
// 使用System.in创建Scanner对象,用于获取标准输入
Scanner sc = new Scanner(System.in);
PrintStream ps = new PrintStream(new FileOutputStream("out.txt"));
// 增加下面一行将只把回车作为分隔符
sc.useDelimiter("\n");
// 判断是否还有下一个输入项
while (sc.hasNext()) {
// 输出输入项
ps.println("键盘输入的内容是:" + sc.next());
}
ps.close();
}
}