整个的代码如下,将要下载的文件地址修改为自己的即可,执行效果如下图所示。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class t { public static void main(String[] args) throws IOException { for (int inx=0;inx!=20;++inx) {
{System.out.print("value of inx : " + inx );}
System.out.print("\n");
if (inx==13){
download();
}
}
}
//执行shell命令,通过wget来下载文件
static void download() throws IOException{
Process process = null;
Runtime rt = Runtime.getRuntime();
try {
process = rt.exec("wget 你要下载的文件");
} catch (IOException e) {
e.printStackTrace(); }
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String b;
while((b=br.readLine())!=null)
{
System.out.println(b);
System.out.println(br.readLine());
}
}
}