作者pzyc79 (五薀皆空)
看板java
标题Re: [问题] Java 路径字串问题
时间Thu Nov 22 15:47:11 2012
※ 引述《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
1F:推 tomoyaken14:"\\s+" 11/22 19:41