在java中执行命令提示符



我在java中执行这些命令,得到以下错误

symbol  : method write(java.lang.String)
location: class java.io.OutputStream
out.write("tabcmd publish C:\Users\c200433\Desktop\Ana\".getBytes()+filename+" --db-username IIP_RBM_USER --db-password Ytpqxsb9dw".getBytes());

String command = "cmd /c start cmd.exe";
    Process child = Runtime.getRuntime().exec(command);
    OutputStream out = child.getOutputStream();
out.write("tabcmd publish C:\Users\c200433\Desktop\Ana\".getBytes()+filename+" --db-username IIP_R --db-password Ytb9dw".getBytes());
How Do i resolve this issue.

OutputStream除了byte[],而不是String:

out.write("tabcmd publish C:\Users\c200433\Desktop\Ana\".getBytes() +
           filename + " --db-username IIP_R --db-password Ytb9dw".getBytes());

我认为javac不允许"...".getBytes() + String,因为它是byte[] + String。并为NumberStringboolean定义了+算子。不是byte[]

相反,您必须:

  • 或者在流上使用PrintStream
  • 要么先连接到一个String,然后对结果调用getBytes

使用PrintStream:

try (PrintStream ps = new PrintStream(child.getOutputStream())) {
  ps.append("tabcmd publish C:\Users\c200433\Desktop\Ana\")
    .append(filename)
    .append(" --db-username IIP_R --db-password Ytb9dw");
}

使用String级联:

out.write( ("tabcmd publish C:\Users\c200433\Desktop\Ana\" +
            filename + " --db-username IIP_R --db-password Ytb9dw").getBytes(Charset.forName("utf-8")));

我使用getBytes(Charset)而不是getBytes(),但这取决于您的流程(例如:它接受UTF-8吗?)。但是,您必须记住,Java字符串是Unicode序列,因此,getBytes()可能使用Windows cp1252(Windows上的默认值)。

我没有签入PrintStream源代码,但如果它使用了没有CharsetgetBytes(),那么使用应该使用具有适当字符集的OutputStreamWriter

最新更新