2016-04-06 4 views
1

自分のシステムのプロセス(つまりタスクマネージャ)を実行してファイルに保存したいが、問題は実行中のプロセスを取得しているが、ファイルに書き込まれていない。 コードは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.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(); 
+1

System.outに書き込むだけで何が得られますか? –

+0

イメージ名PIDセッション名セッション番号Mem使用状況 システムアイドルプロセス0サービス0 24 K システム4サービス0 304 K @ScaryWombat –

+0

このコードに問題はありません。報告されていないエラーはありますか? –

答えて

0

です。私はPrintWriterに移動しました(主に私の意見では、BufferedWriterよりも使いやすいので)。また、読み込みシステムをScannerに移しました。また、Runtime要素をProcessBuilderに移動し、エラーストリームを標準出力にリダイレクトしました。

// 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(); 
関連する問題