使用 java 和命令提示符写入文件
当我从命令提示符运行此命令 ffmpeg -i "C:\user\test.wmv" >C:\user\test.wmv_info.txt 2>&1
时它可以工作,但是当我尝试通过调用命令提示符从 java 文件执行相同操作,它可以正常执行,但不会写入文件。
知道为什么吗?
我的java代码是:
public void getInfoThroughCommandLine(String sourceFilePath) {
try {
String infoFile = sourceFilePath+"_info.txt";
String command = "ffmpeg -i \""
+ sourceFilePath +"\" >"+infoFile+" 2>&1";
// Execute the command
Process process = Runtime.getRuntime().exec("cmd.exe /c start " + command);
logger.info("Executing getInfoThroughCommandLine command: " + command);
// Read the response
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
// Parse the input stream
String line = input.readLine();
System.out.println("ffmpeg execution of: " + sourceFilePath);
while (line != null) {
System.out.println("\t***" + line);
line = input.readLine();
}
// Parse the error stream
line = error.readLine();
System.out.println("Error Stream: " + sourceFilePath);
while (line != null) {
//do somthing
}
} catch (Exception e) {
System.err.println(e);
}
}
when I run this command ffmpeg -i "C:\user\test.wmv" >C:\user\test.wmv_info.txt 2>&1
from command prompt it works but when I try to the same from java file by calling the command prompt it executes all right but does not writes to the file.
Any idea why?
my java code is:
public void getInfoThroughCommandLine(String sourceFilePath) {
try {
String infoFile = sourceFilePath+"_info.txt";
String command = "ffmpeg -i \""
+ sourceFilePath +"\" >"+infoFile+" 2>&1";
// Execute the command
Process process = Runtime.getRuntime().exec("cmd.exe /c start " + command);
logger.info("Executing getInfoThroughCommandLine command: " + command);
// Read the response
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
// Parse the input stream
String line = input.readLine();
System.out.println("ffmpeg execution of: " + sourceFilePath);
while (line != null) {
System.out.println("\t***" + line);
line = input.readLine();
}
// Parse the error stream
line = error.readLine();
System.out.println("Error Stream: " + sourceFilePath);
while (line != null) {
//do somthing
}
} catch (Exception e) {
System.err.println(e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您正在使用 getRuntime().exec() 来执行?
如果是这样,它返回的 Process 对象将允许您访问所执行命令的输入/输出流。只需从中读取并编写您自己的文件即可。
--根据评论进行编辑--
在
“cmd.exe /c start”+命令
中启动将在单独的窗口中启动程序,我猜流该过程的一部分附加到该窗口。尝试将其删除。 IE
I assume you're using
getRuntime().exec()
to execute?If so the
Process
object returned by it would be the one giving you access to in/out streams of the command you execute. Just read from it and write your own file.-- edit based on discussion via comments --
start in
"cmd.exe /c start " + command
would start the program in a separate window, and I guess the streams of the process are attached to that window.Try removing it. I.e.
>
和2>&1
是 shell 运算符,告诉 shell 重定向命令的输出 (ffmpeg -i "C:\user\ test.wmv"
) 到特定文件 (C:\user\test.wmv_info.txt
)。这些运算符在 Java 中不起作用,在 Java 中,您必须显式获取标准输出和标准错误(通过 Process#getInputStream() 和 Process#getErrorStream() 分别 - 我知道这看起来是倒退的)并将这些流的输出写入您自己的文件中。
The
>
and2>&1
are shell operators that tells the shell to redirect the output of your command (ffmpeg -i "C:\user\test.wmv"
) to a specific file (C:\user\test.wmv_info.txt
).Those operators do not work in Java, in Java, you have to explicitly take the standard output and standard error (via Process#getInputStream() and Process#getErrorStream() respectively - I know it seems backwards) and write the output of those streams to file yourself.
重定向由命令 shell 处理——即由
CMD.EXE
处理——如果您只是将上面的行输入到Runtime.exec()
,它就会成功没完没了。您可以安排将此命令行发送到CMD.EXE
(这很难正确执行),或者您可以通过读取进程输出和错误流并存储来自己在 Java 中进行重定向。将数据写入文件。Redirection is handled by the command shell -- i.e., by
CMD.EXE
-- and if you're just feeding the line above toRuntime.exec()
, it won't be done. You can either arrange to have this command line send toCMD.EXE
-- which is complicated to get right -- or you can do the redirection yourself in Java by reading the process output and error streams and storing the data to a file.也许您可以将该命令写入 .bat 文件并运行它?不是最干净的解决方案,但它可能有效。
Maybe you could write that command out to a .bat file and run that? Not the cleanest solution but it might work.