Read stdout from command line with BufferedReader
807588Apr 3 2009 — edited Apr 24 2009I have written a Java program that utilizes GNU's wget to retrieve web site content files. I have a major flaw in my setup though as I do not always want to download a very large website as they bog down my RAM and disk space. I do want to download all the content files from over 99% of the websites given to my program.
My solution is to read the standard output from wget and if it runs for a given threshold then destroy the process. For some reason this does not work though. I have to terminate the wget process myself before seeing any output. I was wondering if anyone would be willing to look at my code and tell me what is wrong. I also do not understand why the std error is what wget normally outputs when it works.
Use www.google.com as a good example, but kill the process wget if your code does not do so or it will download many many files. Note that I am using five levels of depth for downloads in the wget command line options. Thanks.
int WaitTime = 50;
int TotalTime = 0;
Process pr = rt.exec("wget -t 1 -T 5 -l 5 -np -r " + url);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command of all:\n");
while ((String s = stdInput.readLine()) != null) {
System.out.println(s);
TotalTime += WaitTime;
Thread.sleep(WaitTime);
if(TotalTime > 10000){
System.out.println("Destroy process : " + pr.toString());
pr.destroy();
break;
}
}
TotalTime = 0;
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any) of all:\n");
while ((String s2 = stdError.readLine()) != null) {
System.out.println(s2);
TotalTime += WaitTime;
Thread.sleep(WaitTime);
if(TotalTime > 10000){
System.out.println("Destroy process : " + pr.toString());
pr.destroy();
break;
}
}