即使文件/文件夹名称在单词之间有多个空格也可以。
//In this example there are 3 spaces between "GAME" and "OF" and 2 spaces between "OF" and "Thrones"
String onlyPath = "D:\\GAME OF Thrones";
String selectPath = "/select," + onlyPath;
//START: Strip one SPACE among consecutive spaces
LinkedList list = new LinkedList<>();
StringBuilder sb = new StringBuilder();
boolean flag = true;
for (int i = 0; i < selectPath.length(); i++) {
if (i == 0) {
sb.append(selectPath.charAt(i));
continue;
}
if (selectPath.charAt(i) == ' ' && flag) {
list.add(sb.toString());
sb.setLength(0);
flag = false;
continue;
}
if (!flag && selectPath.charAt(i) != ' ') {
flag = true;
}
sb.append(selectPath.charAt(i));
}
list.add(sb.toString());
list.addFirst("explorer.exe");
//END: Strip one SPACE among consecutive spaces
//Output List
for (String s : list) {
System.out.println("string:"+s);
}
/*output of above loop
string:explorer.exe
string:/select,D:\GAME
string: OF
string: Thrones
*/
//Open in Explorer and Highlight
Process p = new ProcessBuilder(list).start();