java中文件的运行时进程

java中文件的运行时进程,java,Java,我想获取我的系统的运行进程,即任务管理器,并将它们保存在文件中,但问题是我获取运行进程,但它们没有写入文件 我的代码是 BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\Zeeshan Nisar\\Desktop\\process.txt", true)); // Get process and make reader from that process Process p = Runtime.getRunt

我想获取我的系统的运行进程,即任务管理器,并将它们保存在文件中,但问题是我获取运行进程,但它们没有写入文件 我的代码是

BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\Zeeshan Nisar\\Desktop\\process.txt", true));

// Get process and make reader from that process
Process p = Runtime.getRuntime().exec("tasklist.exe");
BufferedReader s = new BufferedReader(new InputStreamReader(p.getInputStream()));

// While reading, print.
String input = null;
while ((input = s.readLine()) != null) {
    out.write(input);
    out.newLine();
}

out.close();

试试这个。我把它移到了PrintWriter(主要是因为它比BufferedWriter更容易使用,至少在我看来是这样),并把阅读系统移到了扫描仪上(在我看来也是因为它更容易使用)。我还将运行时元素移动到ProcessBuilder中,并将错误流重定向到standard out中

// Make a PrintWriter to write the file
PrintWriter printer = new PrintWriter("process.txt");

// Make a process builder so that we can execute the file effectively
ProcessBuilder procBuilder = new ProcessBuilder();
procBuilder.command(new String[] { "your", "commands" });   // Set the execution target of the PB
procBuilder.redirectErrorStream(true);  // Make it slam the error data into the standard out data
Process p = procBuilder.start();    // Start

// Scan the process
Scanner scan = new Scanner(p.getInputStream());
while (scan.hasNextLine()) {
    printer.println(scan.nextLine());   // While it provides data, print to file
}

// Close everything to prevent leaks
scan.close();
printer.close();

试试这个。我把它移到了PrintWriter(主要是因为它比BufferedWriter更容易使用,至少在我看来是这样),并把阅读系统移到了扫描仪上(在我看来也是因为它更容易使用)。我还将运行时元素移动到ProcessBuilder中,并将错误流重定向到standard out中

// Make a PrintWriter to write the file
PrintWriter printer = new PrintWriter("process.txt");

// Make a process builder so that we can execute the file effectively
ProcessBuilder procBuilder = new ProcessBuilder();
procBuilder.command(new String[] { "your", "commands" });   // Set the execution target of the PB
procBuilder.redirectErrorStream(true);  // Make it slam the error data into the standard out data
Process p = procBuilder.start();    // Start

// Scan the process
Scanner scan = new Scanner(p.getInputStream());
while (scan.hasNextLine()) {
    printer.println(scan.nextLine());   // While it provides data, print to file
}

// Close everything to prevent leaks
scan.close();
printer.close();

如果您只需写入System.out,您会得到什么输出?映像名称PID会话名称会话#内存使用系统空闲进程0服务0 24 K系统4服务0 304 K@scarywombat此代码没有任何问题。没有报告任何错误吗?没有,先生,没有@scarywombati文件是创建的,并且是空的,还是根本没有创建文件?您是否尝试将路径设置为一个没有空格的目录?如果只是写入System.out,您会得到什么输出?映像名称PID会话名称会话#Mem使用系统空闲进程0服务0 24 K系统4服务0304 K@scarywombat无法看到此代码存在任何问题。没有报告任何错误吗?没有,先生,没有@scarywombati文件是创建的,并且是空的,还是根本没有创建文件?您是否尝试将路径设置为没有空格的目录?