cmd打开java文件夹_Java用CMD打开指定文件和文件夹
1、打開(kāi)可執(zhí)行程序(exe等)
public static void main(final String[] args) throws IOException {
openWindowsExe();
openExe();
openFile();
}
// 用 Java 調(diào)用windows系統(tǒng)的exe文件,比如notepad,calc之類
public static void openWindowsExe() {
final Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
final String command = "notepad";// 記事本
process = runtime.exec(command);
} catch (final Exception e) {
System.out.println("Error win exec!");
}
}
// 調(diào)用其他的可執(zhí)行文件,例如:自己制作的exe,或是 下載 安裝的軟件.
public static void openExe() {
final Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec("C:\\Program Files\\Notepad++\\notepad++.exe");
} catch (final Exception e) {
System.out.println("Error exec!");
}
}
// 打開(kāi)其他任意格式的文件,比如txt,word等
public static void openFile() {
final Runtime runtime = Runtime.getRuntime();
Process process = null;//
final String cmd = "rundll32 url.dll FileProtocolHandler file://F:\\ECT項(xiàng)目資料\\建立EMF工程.txt";
try {
process = runtime.exec(cmd);
} catch (final Exception e) {
System.out.println("Error exec!");
}
}
2、調(diào)用cmd打開(kāi)目錄
public class OpenDirectory {
public static void main(String[] args) throws IOException {
String[] cmd = new String[5];
String url = "D:\\ZTarget";
cmd[0] = "cmd";
cmd[1] = "/c";
cmd[2] = "start";
cmd[3] = " ";
cmd[4] = url;
Runtime.getRuntime().exec(cmd);
}
}
3、直接調(diào)用cmd命令
public static void main(String[] args) throws IOException {
String[] command = {"cmd","/c", "move G:/a.txt G:/Step_tmp.txt" };
Process ps = Runtime.getRuntime().exec(command );
}
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的cmd打开java文件夹_Java用CMD打开指定文件和文件夹的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java中使用递归方法删除_删除和拷贝文
- 下一篇: java美元兑换,(Java实现) 美元