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);  
    }  
  }
 
String   command="cmd   /C   dir";
此句在不同操作系统下命令格式不一样,linux下只需要String   command="ls";