利用Apache commons exec 实现指定应用打开对应文件
2017-07-26 15:55
471 查看
用到的jar包
commons-exec-1.3-javadoc.jar
commons-exec-1.3-sources.jar
commons-exec-1.3-test-sources.jar
commons-exec-1.3-tests.jar
commons-exec-1.3.jar
commons-io-2.4.jar
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.output.ByteArrayOutputStream;
public class Test {
public static void main(String[] args) {
Test exec = new Test();
exec.notepadReadFile("d:/a.txt");
}
public void notepadReadFile(String filePath) {
String command = "notepad.exe " + filePath;
try{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
//命令行处理
CommandLine commandline = CommandLine.parse(command);
//进行执行体
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
//利用监视狗来设置超时
ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
exec.setWatchdog(watchdog);
PumpStreamHandler streamHandler = new PumpStreamHandler(
outputStream,errorStream);
exec.setStreamHandler(streamHandler);
exec.execute(commandline);//执行
String out = outputStream.toString("gbk");
String error = errorStream.toString("gbk");
System.out.println(out);
System.err.println(error);
}catch (Exception e) {
e.printStackTrace();
}
}
}
commons-exec-1.3-javadoc.jar
commons-exec-1.3-sources.jar
commons-exec-1.3-test-sources.jar
commons-exec-1.3-tests.jar
commons-exec-1.3.jar
commons-io-2.4.jar
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.output.ByteArrayOutputStream;
public class Test {
public static void main(String[] args) {
Test exec = new Test();
exec.notepadReadFile("d:/a.txt");
}
public void notepadReadFile(String filePath) {
String command = "notepad.exe " + filePath;
try{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
//命令行处理
CommandLine commandline = CommandLine.parse(command);
//进行执行体
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
//利用监视狗来设置超时
ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
exec.setWatchdog(watchdog);
PumpStreamHandler streamHandler = new PumpStreamHandler(
outputStream,errorStream);
exec.setStreamHandler(streamHandler);
exec.execute(commandline);//执行
String out = outputStream.toString("gbk");
String error = errorStream.toString("gbk");
System.out.println(out);
System.err.println(error);
}catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 利用Apache commons exec 实现指定应用打开对应文件
- 利用apache的Commons-fileupload和Ajax实现文件上传进度条
- android 文件关联的方法 实现点击文件打开指定应用软件
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- 利用Jakarta commons fileupload组件实现多文件上传
- Apache Commons fileUpload实现文件上传
- java web 利用 commons-fileupload-1.2 实现文件上传 解决 中文乱码& 非文件属性的读取
- [收藏]利用Jakarta commons fileupload组件实现多文件上传 by hbcui1984
- 利用discuz实现PHP大文件上传应用实例代码
- 利用commons-fileupload 实现文件上传终极版及commons-fileupload和commons-io包关系
- 用C#实现利用F1打开帮助文件
- 非MFC,利用window api OPENFILENAME实现定制打开文件对话框
- 利用Dataview实现搜索指定目录下的所有文件,以指定的条件排序(可以按文件名升降序,最后修改时间升降序)
- 利用commons-fileupload实现文件上传
- 利用commons-fileupload实现多个文件上传
- 利用 Commons-Fileupload 实现文件上传
- 利用Jakarta commons fileupload组件实现多文件上传
- 利用Jakarta commons fileupload组件实现多文件上传
- 利用Jakarta commons fileupload组件实现多文件上传
- java web 利用 commons-fileupload-1.2 实现文件上传 解决 中文乱码& 非文件属性的读取