import java.io.*;
class PT
{
public static void main(String[] args) throws IOException
{
String filename="out.txt";
if(args.length>0){
filename=args[0];
}
String command="cmd /C dir";
Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
PrintStream ps=new PrintStream(new FileOutputStream(filename));
String inline;
while(null!=(inline=br.readLine())){
ps.println(inline);
}
System.out.println("a command result has been readed to a file "+filename);
}
}
class PT
{
public static void main(String[] args) throws IOException
{
String filename="out.txt";
if(args.length>0){
filename=args[0];
}
String command="cmd /C dir";
Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
PrintStream ps=new PrintStream(new FileOutputStream(filename));
String inline;
while(null!=(inline=br.readLine())){
ps.println(inline);
}
System.out.println("a command result has been readed to a file "+filename);
}
}
而不是存入文件?谢谢!
class PT
{
public static void main(String[] args) throws IOException
{
String filename="out.txt";
if(args.length>0){
filename=args[0];
}
String command="cmd /C dir";
Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
while(null!=(inline=br.readLine())){
sb.append(inline).append("\n");
}
System.out.println(sb.toString());
}
}
import java.io.*;
class PT
{
public static void main(String[] args) throws IOException
{
String command="cmd /C dir";
Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
while(null!=(inline=br.readLine())){
sb.append(inline).append("\n");
}
System.out.println(sb.toString());
}
}
String command="ls"; 不能String command="cmd /C ls";
不是说JAVA是一次编译,到处使用的吗?
可是你要调用操作系统命令呀,当然和操作系统有关