java怎么调用批处理文件

44次阅读
没有评论

共计 587 个字符,预计需要花费 2 分钟才能阅读完成。

在 Java 中,可以使用 Runtime 类的 exec() 方法执行批处理文件。

以下是一个简单的示例:

public class CallBatchFile {public static void main(String[] args) {try {// 调用批处理文件
            Process process = Runtime.getRuntime().exec("path_to_batch_file.bat");

            // 获取批处理文件的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {System.out.println(line);
            }

            // 等待批处理文件执行完毕
            int exitCode = process.waitFor();
            System.out.println("Exit Code: " + exitCode);
        } catch (IOException | InterruptedException e) {e.printStackTrace();
        }
    }
}

请将 path_to_batch_file.bat 替换为实际的批处理文件路径。运行以上代码将调用批处理文件并打印出其输出。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-09发表,共计587字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)