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

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文件

扫描二维码关注公众号,回复: 741759 查看本文章
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;
	}

猜你喜欢

转载自hw1287789687.iteye.com/blog/2253323