使用 java 和命令提示符写入文件

发布于 2024-12-10 18:17:22 字数 1480 浏览 0 评论 0原文

当我从命令提示符运行此命令 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∝单色的世界 2024-12-17 18:17:22

我假设您正在使用 getRuntime().exec() 来执行?

如果是这样,它返回的 Process 对象将允许您访问所执行命令的输入/输出流。只需从中读取并编写您自己的文件即可。

--根据评论进行编辑--

“cmd.exe /c start”+命令中启动将在单独的窗口中启动程序,我猜流该过程的一部分附加到该窗口。

C:\Users\z000dgqd>start /?
Starts a separate window to run a specified program or command.
........

尝试将其删除。 IE

    // Change this:
    Process process = Runtime.getRuntime().exec("cmd.exe /c start " + command);
    // to this
    Process process = Runtime.getRuntime().exec("cmd.exe /c " + command);

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.

C:\Users\z000dgqd>start /?
Starts a separate window to run a specified program or command.
........

Try removing it. I.e.

    // Change this:
    Process process = Runtime.getRuntime().exec("cmd.exe /c start " + command);
    // to this
    Process process = Runtime.getRuntime().exec("cmd.exe /c " + command);
凡间太子 2024-12-17 18:17:22

>2>&1 是 shell 运算符,告诉 shell 重定向命令的输出 (ffmpeg -i "C:\user\ test.wmv") 到特定文件 (C:\user\test.wmv_info.txt)。

这些运算符在 Java 中不起作用,在 Java 中,您必须显式获取标准输出和标准错误(通过 Process#getInputStream()Process#getErrorStream() 分别 - 我知道这看起来是倒退的)并将这些流的输出写入您自己的文件中。

The > and 2>&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.

层林尽染 2024-12-17 18:17:22

重定向由命令 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 to Runtime.exec(), it won't be done. You can either arrange to have this command line send to CMD.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.

最终幸福 2024-12-17 18:17:22

也许您可以将该命令写入 .bat 文件并运行它?不是最干净的解决方案,但它可能有效。

Maybe you could write that command out to a .bat file and run that? Not the cleanest solution but it might work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文