Java 执行命令行并在询问时始终替换 [Y/N]

Java execute command line and always replace when it asks [Y/N]

我在我的 Java 程序中调用 java.lang.Runtime.exec(...) 到 运行 一个命令,比如:

ffmpeg -i input.mp4 output.mp4.

这只是传递给我的函数 运行:

    private static void RunCommand(String command) throws InterruptedException {
        try {
            // Execute command
            Process proc = Runtime.getRuntime().exec(command);
}
}

一切正常。但我的问题是处理文件已经存在的情况,所以询问是否应该替换它:

File 'output.avi' already exists. Overwrite ? [y/N]

忽略它的最简单方法是什么(总是假设 y 并替换)?也许通过代码中的 Java 或 FFMPEG 命令本身?

添加 -y 作为参数:

ffmpeg -y -i input.mp4 output.mp4

根据documentation

  • -y 不询问就覆盖输出文件。

  • -n不覆盖输出文件,如果指定的输出文件已经存在则立即退出