处理文件夹有空格问题及按系统方式打开文件

[size=medium]取得文件路径:
[color=violet]String filePath= xxx.class.getResource("/").getPath();[/color]
处理:
[color=violet]filePath=URLDecoder.decode(filePath);[/color]
完成.

按系统方式打开文件:
[color=violet]Runtime.getRuntime().exec("cmd /c start "+filePath);[/color]

=================================================================================
[color=red]修改:[/color]

原用上面的方式按系统方式打开文件,结果出现空格问题,不好弄。最后想到Dos的8.3短文件名方式不会出现空格,,于是写了一个求短文件名的方法:
        /**
* 8.3DOS短文件名规则
* @param srcFileName
* @return
*/
private static String get8Dot3Name(String srcFileName) {
StringBuffer sb;
srcFileName = srcFileName.toUpperCase();
if (srcFileName.getBytes().length > 8
|| ((srcFileName.getBytes().length < 8) && srcFileName
.indexOf(" ") >= 0)) {

srcFileName = srcFileName.replaceAll(" ", "");
sb = new StringBuffer(srcFileName);
while (sb.indexOf(".") != sb.lastIndexOf(".")) {
sb.setCharAt(sb.indexOf("."), ' ');
}
srcFileName = sb.toString().replaceAll("[ $<>;,=\"\\[\\]]", "");
if (srcFileName.lastIndexOf('.') == srcFileName.length() - 1) {
srcFileName = srcFileName
.substring(0, srcFileName.length() - 1);
}
String name = null;
String type = null;
if (srcFileName.lastIndexOf(".") > -1) {

name = srcFileName.substring(0, srcFileName.indexOf("."));
type = srcFileName.substring(srcFileName.indexOf("."));

if (name.getBytes().length > 8) {
name = cut6Char(name);
}
if (type.getBytes().length > 4) {
type = type.substring(0, 4);
}
} else {
if (srcFileName.getBytes().length > 8) {
name = cut6Char(srcFileName);
} else {
name = srcFileName;
}
}
name += "~1";
if (type != null) {
name += type;
}
return srcFileName = name;
}
return srcFileName;
}

private static String cut6Char(String name) {
byte[] bt = new byte[6];
byte nameBytes[] = name.getBytes();
for (int i = 0; i < 6; i++) {
bt[i] = nameBytes[i];
}
name = new String(bt);
return name;
}
/**
* 取得短文件名
*/
public static String getShortName(String filePath) {
filePath=filePath.replaceAll("\\\\", "/");
String[] str = filePath.split("/");
for (int i = 0; i < str.length; i++) {
str[i] = get8Dot3Name(str[i]);
}
StringBuffer sbf = new StringBuffer();
for (int i = 0; i < str.length; i++) {
sbf.append(str[i] );
if(i!=str.length-1){
sbf.append("\\");
}
}
return sbf.toString();
}


好。。。到这里先试了一把。。。——!结果出人意料。。。有的可以打开有的打不开。。。
原来我这个方法是只针对一个路径来做的。。也就是说相同目录下有多个文件可以得到这个路径,
于是上网又找,终于找到了。。 :D
[color=violet]1.如何执行非系统注册的命令比如Windows的dir
请使用命令 "cmd /c dir"
其他情况类似
2.如何打开带空格的外部文件或文件夹
最基本的打开外部文件的方式 "cmd /c start 文件",若文件名有空格,则会出现错误,请使用以下方式解决
String[] cmd = new String[5];
cmd[0] = "cmd";
cmd[1] = "/c";
cmd[2] = "start";
cmd[3] = " ";
cmd[4] = filePath;
Process process = Runtime.getRuntime().exec(cmd);

或;
Process process = Runtime.getRuntime().exec("cmd /c start \"\" \"E:\\kk sd\\www.txt\"");

因为按找文档说明 start 命令之后首先是[title],再是[filepath],所以将title设置为 " "


3.调用外部程序来打开一个相应的文件
比如我们要使用Editplus来打开一个远程机器上的文件,可以这样

Runtime.getRuntime().exec("D:\\EditPlus 2\\EditPlus.exe"+" "+\\\\172.16.1.6\\server1\\SystemErr.log)即
Runtime.getRuntime().exec("外部程序位置"+" "+"要打开的文件").
以此方式可以忽略空格的问题

4.截取控制台的信息.
使用JAVA输入流的方式.
Runtime.getRuntime().exec(..)获取的Process;
Process pro = Runtime.getRuntime().exec(..);
InputStreamReader isr = new InputStreamReader (pro.getInputStream());
//todo 使用输入流进行操作就可以了.[/color]


另外JDK6也提供了一种方式
Desktop dsk=DeskTop.getDesktop();
dsk.open(new File(filePath));
[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值