java浏览文件夹_Java程序打开浏览器和文件夹

这篇博客介绍了如何使用Java进行系统交互,包括利用Desktop类打开默认浏览器浏览指定URL,使用Runtime执行命令打开文件夹以及借助CMD启动notepad编辑TXT文件。内容详细展示了相关代码实现,适用于Java桌面应用开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java程序打开浏览器和文件夹

(1)打开网址

//启用系统默认浏览器来打开网址。

try {

URI uri = new URI("file:///"+fileFullPath.replaceAll("\\\\", "/"));

Desktop.getDesktop().browse(uri);

} catch (URISyntaxException e2) {

e2.printStackTrace();

// GUIUtil23.errorDialog(e2);

ToastMessage.toast(e2.getMessage(),3000,Color.red);

} catch (IOException e2) {

e2.printStackTrace();

// GUIUtil23.errorDialog(e2);

ToastMessage.toast(e2.getMessage(),3000,Color.red);

}

(2)打开文件所在文件夹

String pathStr=this.tf.getText();

if(!ValueWidget.isNullOrEmpty(pathStr)){

FileUtils.open_directory(SystemHWUtil.getParentDir(pathStr));

}

FileUtils.open_directory实现体为:

/***

*

* @param folder

* : directory

*/

public static void open_directory(Object folderObj) {

if (ValueWidget.isNullOrEmpty(folderObj)) {

return;

}

File file = null;

/*if (folderObj instanceof JTextField) {

JTextField tf = (JTextField) folderObj;

file = new File(tf.getText());

} else */if (folderObj instanceof String) {

file = new File((String) folderObj);

} else {

file = (File) folderObj;

}

if (!file.exists()) {

return;

}

Runtime runtime = null;

try {

runtime = Runtime.getRuntime();

if (SystemHWUtil.isWindows) {

runtime.exec("cmd /c start explorer " + file.getAbsolutePath());

} else {

runtime.exec("nautilus " + file.getAbsolutePath());

}

} catch (IOException ex) {

ex.printStackTrace();

} finally {

if (null != runtime) {

runtime.runFinalization();

}

}

}

(3)使用notepad打开txt文件

final String pathStr=this.tf.getText();

if(!ValueWidget.isNullOrEmpty(pathStr)){

new Thread(new Runnable() {

@Override

public void run() {

String result;

try {

result = CMDUtil.execute(new String[]{"cmd", "/c", "notepad", pathStr}, null, null);

System.out.println(result);

} catch (IOException ex) {

ex.printStackTrace();

}

}

}).start();

}

CMDUtil.execute实现如下:

/***

*

* @param command

* @param cmdFolder : 命令的路径

* @param charset

* @return

* @throws IOException

*/

public static String execute(String[]command,String cmdFolder,String charset) throws IOException{

BufferedReader reader = null;

Process p = null;

String errorInputStr = null;

try

{

// String commandFolder=;

if(ValueWidget.isNullOrEmpty(charset)){

charset=SystemHWUtil.CURR_ENCODING;

}

if(ValueWidget.isNullOrEmpty(cmdFolder)){

p = Runtime.getRuntime().exec(command);

}else{

File cmdFolder2=null;

if(!ValueWidget.isNullOrEmpty(cmdFolder)){

cmdFolder2=new File(cmdFolder/*命令的所在目录*/);

p = Runtime.getRuntime().exec(command, null,cmdFolder2/*命令的路径*/);//)

}else{

p = Runtime.getRuntime().exec(command, null);//)

}

}

reader = new BufferedReader(new InputStreamReader(p

.getInputStream(),charset));

errorInputStr=FileUtils.getFullContent4(p.getErrorStream(),charset);

if(!ValueWidget.isNullOrEmpty(errorInputStr)){

System.out.println("error:"+errorInputStr);

}

StringBuilder sb = new StringBuilder();

String readedLine = null;

try

{

while ((readedLine = reader.readLine()) != null)

{

sb.append(readedLine);

sb.append("\r\n");

}

}

catch (IOException e)

{

e.printStackTrace();

}

finally

{

try

{

reader.close();

p.destroy();

}

catch (IOException e)

{

e.printStackTrace();

}

}

String content = sb.toString();

System.out.println(content);

}

catch (IOException e)

{

e.printStackTrace();

throw e;

}

return errorInputStr;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值