天天看點

Java程式打開浏覽器和檔案夾

(1)打開網址

Java程式打開浏覽器和檔案夾

//啟用系統預設浏覽器來打開網址。  

        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) {  

        }  

(2)打開檔案所在檔案夾

Java程式打開浏覽器和檔案夾

string pathstr=this.tf.gettext();  

            if(!valuewidget.isnullorempty(pathstr)){  

                fileutils.open_directory(systemhwutil.getparentdir(pathstr));  

            }  

fileutils.open_directory實作體為:

Java程式打開浏覽器和檔案夾

/*** 

     *  

     * @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()) {  

        runtime runtime = null;  

            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檔案

Java程式打開浏覽器和檔案夾

final string pathstr=this.tf.gettext();  

                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實作如下:

Java程式打開浏覽器和檔案夾

     * @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;