JAVA IO流 从控制台输入以下命令并解析 复制文件copy d:/a.txt e:/ 将D盘下的文件a.txt复制到E盘
答案:1 悬赏:70 手机版
解决时间 2021-11-27 23:36
- 提问者网友:听门外雪花风
- 2021-11-27 18:08
JAVA IO流 从控制台输入以下命令并解析 复制文件copy d:/a.txt e:/ 将D盘下的文件a.txt复制到E盘
最佳答案
- 五星知识达人网友:走死在岁月里
- 2021-11-27 18:15
基本操作如下:
你要补充的是:
1 动态输入内容
2 异常处理
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
public class Command {
private java.lang.Process p;
private InputStream is;
private OutputStream os;
private BufferedWriter bw;
private BufferedReader br;
private ProcessBuilder pb;
private InputStream stdErr;
public Command() {
}
//获取Process的输入,输出流
public void setCmd(String cmd) {
try {
p = Runtime.getRuntime().exec(cmd);
os = p.getOutputStream();
is = p.getInputStream();
stdErr = p.getErrorStream();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
//向Process输出命令
public void writeCmd(String cmd) {
try {
bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(cmd);
bw.newLine();
bw.flush();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//读出Process执行的结果
public String readCmd() {
StringBuffer sb = new StringBuffer();
br = new BufferedReader(new InputStreamReader(is));
String buffer = null;
try {
while ((buffer = br.readLine()) != null) {
sb.append(buffer + "
");
}
System.out.println(p.waitFor());
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
//将命令一股脑塞入list中
public LinkedList doCmd(LinkedList lists) {
LinkedList list = new LinkedList();
for (String s : lists) {
writeCmd(s);
list.add(readCmd());
}
return list;
}
public static void main(String[] args) {
Command cmd = new Command();
cmd.setCmd("cmd");
cmd.writeCmd("copy d:\a.txt e:\ ");
System.out.println(cmd.readCmd());
}
}追问要有输入 然后判断命令,参数是否正确的 要用Scanner编写
你要补充的是:
1 动态输入内容
2 异常处理
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
public class Command {
private java.lang.Process p;
private InputStream is;
private OutputStream os;
private BufferedWriter bw;
private BufferedReader br;
private ProcessBuilder pb;
private InputStream stdErr;
public Command() {
}
//获取Process的输入,输出流
public void setCmd(String cmd) {
try {
p = Runtime.getRuntime().exec(cmd);
os = p.getOutputStream();
is = p.getInputStream();
stdErr = p.getErrorStream();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
//向Process输出命令
public void writeCmd(String cmd) {
try {
bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(cmd);
bw.newLine();
bw.flush();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//读出Process执行的结果
public String readCmd() {
StringBuffer sb = new StringBuffer();
br = new BufferedReader(new InputStreamReader(is));
String buffer = null;
try {
while ((buffer = br.readLine()) != null) {
sb.append(buffer + "
");
}
System.out.println(p.waitFor());
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
//将命令一股脑塞入list中
public LinkedList
LinkedList
for (String s : lists) {
writeCmd(s);
list.add(readCmd());
}
return list;
}
public static void main(String[] args) {
Command cmd = new Command();
cmd.setCmd("cmd");
cmd.writeCmd("copy d:\a.txt e:\ ");
System.out.println(cmd.readCmd());
}
}追问要有输入 然后判断命令,参数是否正确的 要用Scanner编写
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯