Re: [問題] Java 路徑字串問題

看板java作者 (五薀皆空)時間13年前 (2012/11/22 15:47), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《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
"\\s+"
11/22 19:41, 1F
文章代碼(AID): #1GhTY2n4 (java)
文章代碼(AID): #1GhTY2n4 (java)