这样一段java代码:
// 最初的
/*private static final String FILE_COMMAND_WIN = "file.exe %s";
private static final String FILE_COMMAND_LINUX = "file %s";*/
// 最终的
private static final String FILE_COMMAND_WIN = "file.exe";
private static final String FILE_COMMAND_LINUX = "file"
public static String getFileCommandResult(String file) throws IOException {
Process process = null;
InputStream in = null;
InputStreamReader reader = null;
BufferedReader bufferedReader = null;
String result = null;
try {
String command;
if (Util.isWindows())
command = FILE_COMMAND_WIN;
else
command = FILE_COMMAND_LINUX;
//(0)最初的设计,不能解决file中特殊字符的问题,path和文件名字有空格或特殊字符,程序会在这里hang住
//process = Runtime.getRuntime().exec(String.format(command, file));
//(1)这样程序不会hang住,但是shell命令提示找不到这个文件
/*File oldFile = new File(file);
String temp = oldFile.toURI().getRawPath();
System.out.println(temp);*/
//(2)这样程序程序也会hang住
/*String temp = file.replace(" ", "\" \"");
System.out.println(temp);
process = Runtime.getRuntime().exec(String.format(command, temp));*/
//最终的。exec的重载输入可以是个字符数组
//将命令和参数分开,避免exec自己以空格去划分。
String[] commands = new String[2];
commands[0] = command;
commands[1] = file;
//System.out.println(commands.toString());
process = Runtime.getRuntime().exec(commands);
in = process.getInputStream();
reader = new InputStreamReader(in);
bufferedReader = new BufferedReader(reader);
result = bufferedReader.readLine();
} finally {
if (bufferedReader != null)
bufferedReader.close();
if (process != null)
process.destroy();
}
return result;
}
reference:
http://hi.baidu.com/%D5%D4%B7%F6%B7%E7/blog/item/f10ef7ece09e37c42f2e21eb.html
http://www.iteye.com/problems/9241
http://hi.baidu.com/mikeymouse01/blog/item/7eae62f2fd6b6940342accc9.html
http://flyeagle.iteye.com/blog/406487
http://dingbuoyi.iteye.com/blog/836721
Java 文件路径中特殊符号问题
http://wisdombase.net/wiki/index.php?title=Java_%E6%96%87%E4%BB%B6%E8%B7%AF%E5%BE%84%E4%B8%AD%E7%89%B9%E6%AE%8A%E7%AC%A6%E5%8F%B7%E9%97%AE%E9%A2%98
分享到:
相关推荐
主要介绍了详解Java8与Runtime.getRuntime().availableProcessors(),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...
1,申请root权限Runtime.getRuntime().exec("su"); 2,通过数据输出流DataOutputStream写入pm install命令; 3,最后获取Process进程的返回值int i = process.waitFor();,如果i=0,则表明已获取root权限。
完美解决runtime.exec()执行进程block死锁以及为waitFor设置超时 不需要耗cpu的循环判断exitValue==0 开两个进程搞定
Runtime 执行bat
这里不是通过view来截图,也不是通过底层的framebuffer实现截图,而是采用另外一种方法实现截图,通过Runtime.getRuntime().exec()来实现,并保存在sdcard上,代码很简单。
Java调用Linux命令 调用Runtime.exec方法将产生一个本地的进程,并返回一个Process子类的实例, (注意:Runtime.getRuntime().exec(command)返回的是一个Process类的实例), 该实例可用于控制进程或取得进程的...
runtime.exec("cmd /c start " + url); } catch (IOException e) { e.printStackTrace(); } } /** * 鍦ㄥ欢杩熸寚瀹氱殑绉掓暟鍚庡叧鏈? * * @param sec * 鍏虫満寤惰繜锛屽崟浣嶏細绉? */ ...
主要介绍了Java编程使用Runtime和Process类运行外部程序的方法,结合实例形式分析了java使用Runtime.getRuntime().exec()方法运行外部程序的常见情况与操作技巧,需要的朋友可以参考下
java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。 cmd /c dir 是执行完dir命令后关闭命令窗口。 cmd /k dir 是执行完dir命令后不关闭命令窗口。 cmd /c start dir 会打开一个新...
直接看代码:方法一: 代码如下:Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler //www.jb51.net”); 方法二: 代码如下://判断当前系统是否支持Java AWT Desktop扩展 if(java.awt....
//Process p = Runtime.getRuntime().exec("su"); //然后,在向这个进程的写入要执行的命令,即可达到以root权限执行命令: //dos.flush(); //或者用下面的方式: //Runtime.getRuntime().exec&#...
System.out.println("Runtime.getRuntime().maxMemory()="+Runtime.getRuntime().maxMemory()); 而且确实,现有检测工具底层也是用这个语句来进行检测。要解决这个问题,首先我们需要一个可重复使用的测试用例。因此...
利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,不然复杂命令的执行会有问题。...
动态权限工具类
所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须...
蜂鸣器exe,可用java调用 Runtime.getRuntime().exec("d:\\beep.exe");
java 查看任务管理里面的所有线程 Proces java.lang.Runtime.getRuntime().exec("ipconfig");
java用Runtime.getRuntime().exec(cmd)调用js即可,