woff 發表於 2014-8-15 12:35:27

JAVA 如何在LINUX COPY files時出現PROCESS圖標

有使用過進度條的朋友一定會覺得很不方便
因為要從0~100讀取 浪費時間
因檔案大小也不知道這時間讀寫的完嗎?
如這篇
Java Swing 如何使用進度

於是經過修正
改成此方法
可利用DIALOG準確的抓到讀寫完成的時間processdialog.setTitle("Copying files to USB");
                            processdialog.add(labelimg);
                            processdialog.setLocation(400,250);
                            processdialog.pack(); // Packs the dialog so that the JOptionPane can be seen
                            processdialog.setVisible(true); // Shows the dialog
                            new Thread(){
                                    public void run(){
                                          try{
                                                Process process = null;
                                                BufferedReader input = null;
                                                final Runtime runtime = Runtime.getRuntime();
                                                //extact tar for ext3 file
                                                String tarstring = "tar -xvpf /"+tarpath+"/"+cellValue+".tar -C "+extpatition+"/";
                                                process = runtime.exec(new String[]{"/bin/sh","-c",tarstring});
                                                InputStream stdout = process.getInputStream ();
                                                InputStreamReader osr = new InputStreamReader (stdout);
                                                BufferedReader obr = new BufferedReader (osr);
                                                process.waitFor();
                                             
                                                Thread.sleep(2000);
                                          }catch(Exception e){
                                                    e.printStackTrace();
                                          }finally{
                                                    processdialog.dispose();
                                          }
                                    }
                            }.start();用new Thread這樣包起來就可以了
問甚麼要這樣做呢?
因為dialog其實是非常浪費資源的
如果我程式起始啟動dialog
結束用dialog.dispose();時
dialog圖標根本還沒出來就關閉了
就卡個視窗而以
完整過程圖片



頁: [1]
查看完整版本: JAVA 如何在LINUX COPY files時出現PROCESS圖標