1个回答
展开全部
dir是windows系统的命令,用java亦可实现
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);
}
}
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);
}
}
追问
你这是java代码 我问的是bat实现 不是java实现
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询