Reputation: 135
I have code to Print output of ipconfig command using process builder. But I want to print one Line at a time. The idea is to remove few line from the output which the client doesn't want to see in output. Following is the code.
{
File file=new File("D:\\LC");
String[] command = {"CMD", "/C", "dir"};
ProcessBuilder probuilder = new ProcessBuilder( command );
probuilder.directory(file);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null)
{
out.println(readline);
out.println("<br>");
}
}
Here in While loop readline stores the entire value in single shot and hence I can not remove some info from out put. I want readline to take values one line at a time so that by using loops and conditions i can remove line I don't want to print. Thanks for your support in advance.
Upvotes: 0
Views: 6122
Reputation: 12817
There is a typo in your code, it should be probuilder.start()
not pb.start()
.
I have run the program it works as expected, readLine()
reads line by line.
public static void main(String[] args) throws IOException {
String[] cmd = { "CMD", "/C", "dir" };
ProcessBuilder probuilder = new ProcessBuilder(cmd);
Process p = probuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String readline;
int i = 0;
while ((readline = reader.readLine()) != null) {
System.out.println(++i + " " + readline);
}
}
Output:-
1 Volume in drive C is System
2 Volume Serial Number is 1C25-498E
3
4 Directory of C:\juno\ksaravan\workspace\CommandPrompt
5
6 01/13/2015 12:47 PM <DIR> .
7 01/13/2015 12:47 PM <DIR> ..
8 02/13/2015 10:57 AM 623 .classpath
9 01/05/2015 12:48 PM 389 .project
Upvotes: 1