Re: [問題] Java 路徑字串問題
※ 引述《RichieRich (Richie)》之銘言:
: 當路徑有二個空白時例如a b.mp3
: String str = "c:\\a b.mp3";
: try {
: // System.out.println(str);
: System.out.println("rundll32 SHELL32.DLL,ShellExec_RunDLL \"" +
: str + "\"");
: Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL
: \"" + str + "\"");//+ new File(str));
: } catch (Exception ex) {
: System.out.println(ex);
: }
: 會得到一個windows找不到檔案或路徑的訊息
: 請問除了改路徑外有啥解法 謝謝!
查到的解法:
public class Test {
public static void main(String[] args) {
String str = "c:\\a b.mp3";
String c = "rundll32 SHELL32.DLL,ShellExec_RunDLL \"" + str + "\"";
try {
System.out.println(c);
Runtime.getRuntime().exec(c.split(" "));
} catch (Exception ex) {
System.out.println(ex);
}
}
}
傳字串進去exec()內部是用StringTokenizer分割字串
兩個分割符內如果沒有東西他會無視
split則會視為空字串
所以用split先分好丟進去exec()就會跳過內部的分割方式
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.168.164.24
推
11/22 19:41, , 1F
11/22 19:41, 1F
討論串 (同標題文章)