简体   繁体   English

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

[英]writing to a file using java and command prompt

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. 当我从命令提示符运行此命令ffmpeg -i "C:\\user\\test.wmv" >C:\\user\\test.wmv_info.txt 2>&1时它可以正常工作,但是当我尝试通过调用命令提示符它执行正常但不写入文件。

Any idea why? 知道为什么吗?

my java code is: 我的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);
    }
}

I assume you're using getRuntime().exec() to execute? 我假设您正在使用getRuntime().exec()来执行?

If so the Process object returned by it would be the one giving you access to in/out streams of the command you execute. 如果是这样,它返回的Process对象将允许您访问您执行的命令的输入/输出流。 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. "cmd.exe /c start " + command会在一个单独的窗口中启动程序,我猜这个进程的流程都附加到该窗口。

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

Try removing it. 尝试删除它。 Ie

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

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 ). >2>&1是shell运算符,它告诉shell将命令输出( ffmpeg -i "C:\\user\\test.wmv"ffmpeg -i "C:\\user\\test.wmv"到特定文件( 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. 那些运算符不能用Java工作,在Java中,你必须分别显式地采用标准输出和标准错误(分别通过Process#getInputStream()Process#getErrorStream() - 我知道它似乎倒退了)并写出这些流的输出提交自己。

Redirection is handled by the command shell -- ie, by CMD.EXE -- and if you're just feeding the line above to Runtime.exec() , it won't be done. 重定向由命令shell处理 - 即由CMD.EXE - 如果您只是将上面的行提供给Runtime.exec() ,则无法完成。 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. 您可以安排将此命令行发送到CMD.EXE - 这很复杂 - 或者您可以通过读取进程输出和错误流并将数据存储到文件来自己在Java中进行重定向。

Maybe you could write that command out to a .bat file and run that? 也许你可以将该命令写入.bat文件并运行它? Not the cleanest solution but it might work. 不是最干净的解决方案,但它可能会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM