+ 1
how to redirect output of "tasklist" command in file using java.
i am trying to get output of "tasklist" command which is run on cmd and trying to save output in some file.but, i am getting output in another cmd prompt and file is also creating but file is empty.plzz tell me how to save output to file. import java.awt.*; import java.io.*; public class TaskManager1 { static void run(String cmd) { System.out.print("\nbefore try"); try { Process child = Runtime.getRuntime().exec(cmd); } catch (Exception e) { System.out.println("HEY Buddy ! U r Doing Something Wrong "); e.printStackTrace(); } System.out.print("\noutside catch"); } public static void main(String[] args) { run("cmd /c start cmd.exe /K tasklist > D:\\tasklist.txt"); } }
1 Resposta
0
// I wrote this an year ago and this is one of those reusable functions that i still use when needed. This may help you.
// Note: This is how you need to initialize the file object that you are to send to this method:
// File f = new File("filepathhere");
// Goodluck
//This method is used to add/save the record to the file
public static boolean addRecord(File f, String stringtoSave) throws IOException {
if (!f.exists()) {
f.createNewFile();
}
FileWriter fw = new FileWriter(f.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(stringtoSave);
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
return true;
}