欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)在復(fù)制文件時(shí)使用進(jìn)度條(java實(shí)現(xiàn)進(jìn)度條)

 更新時(shí)間:2014年03月02日 15:04:34   投稿:zxhpj  
在對大文件操作時(shí),可能會(huì)需要些時(shí)間,此時(shí)為用戶提供進(jìn)度條提示是非常常見的一項(xiàng)功能,這樣用戶就可以了解操作文件需要的時(shí)間信息。本實(shí)例為大家介紹了在復(fù)制大的文件時(shí)使用的進(jìn)度條提示,需要注意的是,只有在讀取文件超過2秒時(shí),才會(huì)顯示進(jìn)度條

思路分析:

因?yàn)榧纫胁僮髅姘逵忠羞M(jìn)度條,所以肯定要出現(xiàn)兩個(gè)繼承JFrame類的窗體。
先看被調(diào)用的進(jìn)度條窗體,它不需要手動(dòng)操作,所以類的內(nèi)部實(shí)現(xiàn)一個(gè)方法就可以了。因?yàn)樵O(shè)計(jì)文件操作,所以要捕獲異常。首先根據(jù)要復(fù)制的文件創(chuàng)建File對象,以及根據(jù)復(fù)制后文件的保存地址創(chuàng)建File對象,然后創(chuàng)建FileOutputStream對象,再創(chuàng)建FileInputStream對象,之后是ProgressMonitorInputStream對象,然后讀取文件,如果總耗時(shí)超過2秒,將會(huì)自動(dòng)彈出一個(gè)進(jìn)度監(jiān)視窗口。接下來定義byte數(shù)組,再使用while循環(huán)讀取文件,使用FileOutputStream類的write()方法通過流寫數(shù)據(jù),再使用FileOutputStream類的close()方法關(guān)閉輸出流,最后使用ProgressMonitorInputStream類的close()方法關(guān)閉輸入流??梢娫摲椒ㄐ枰齻€(gè)參數(shù):彈出它的父窗口、要復(fù)制的文件地址以及要復(fù)制到的文件夾。
代碼如下:

ProgressMonitorTest.java:

復(fù)制代碼 代碼如下:

package cn.edu.xidian.crytoll;
import java.io.FileInputStream;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.ProgressMonitorInputStream;
public class ProgressMonitorTest {
     
    public void useProgressMonitor(JFrame frame, String copyPath, String newPath) {
        try {
            File file = new File(copyPath); // 根據(jù)要復(fù)制的文件創(chuàng)建File對象
            File newFile = new File(newPath); // 根據(jù)復(fù)制后文件的保存地址創(chuàng)建File對象
            FileOutputStream fop = new FileOutputStream(newFile); // 創(chuàng)建FileOutputStream對象
            InputStream in = new FileInputStream(file);
            // 讀取文件,如果總耗時(shí)超過2秒,將會(huì)自動(dòng)彈出一個(gè)進(jìn)度監(jiān)視窗口。
            ProgressMonitorInputStream pm = new ProgressMonitorInputStream(
                    frame, "文件讀取中,請稍后...", in);
            int c = 0;
            byte[] bytes = new byte[1024]; // 定義byte數(shù)組
            while ((c = pm.read(bytes)) != -1) { // 循環(huán)讀取文件
                fop.write(bytes, 0, c); // 通過流寫數(shù)據(jù)
            }
            fop.close(); // 關(guān)閉輸出流
            pm.close(); // 關(guān)閉輸入流
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

3. 再看主窗體。JLabel、JTextField神馬的就不用說了,選擇文件和選擇文件夾這兩個(gè)按鈕也是老生常談了,最重要的是“開始復(fù)制”按鈕,將由它來負(fù)責(zé)彈出這個(gè)進(jìn)度條,這就需要開辟一個(gè)新的線程,所以主窗體不僅要繼承JFrame類,還要實(shí)現(xiàn)Runnable接口。

4. 在開始復(fù)制按鈕的具體方法里,首先創(chuàng)建一個(gè)Thread對象作為新的線程,然后調(diào)用該對象的start()方法,重載執(zhí)行run()方法,在該方法中創(chuàng)建一個(gè)進(jìn)度條對象,使用JTextField類的getText()方法獲取要復(fù)制的文件地址和要復(fù)制到的路徑,然后調(diào)用進(jìn)度條類里的方法。

代碼如下:

復(fù)制代碼 代碼如下:

package cn.edu.xidian.crytoll;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
 
public class UserMonitorFrame extends JFrame implements Runnable{
     
    /**
     *
     */
    private static final long serialVersionUID = 8674569541853793419L;
    private JPanel contentPane;
    private JTextField fileField;
    private JTextField searchTextField;
    private JTextField replaceTextField;
    private File file;
    private JTextField textField;
    private JTextField textField_1;
     
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UserMonitorFrame frame = new UserMonitorFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
     
    /**
     * Create the frame.
     */
    public UserMonitorFrame() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 501, 184);
        setTitle("在讀取文件時(shí)使用進(jìn)度條");
        getContentPane().setLayout(null);
         
        JLabel label = new JLabel("\u6587\u4EF6\u5730\u5740\uFF1A");
        label.setBounds(10, 10, 70, 15);
        getContentPane().add(label);
         
        textField = new JTextField();
        textField.setBounds(90, 7, 300, 21);
        getContentPane().add(textField);
        textField.setColumns(10);
         
        JButton button = new JButton("\u9009\u62E9\u6587\u4EF6");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setBounds(400, 6, 93, 23);
        getContentPane().add(button);
         
        JLabel label_1 = new JLabel("\u590D\u5236\u5730\u5740\uFF1A");
        label_1.setBounds(10, 40, 70, 15);
        getContentPane().add(label_1);
         
        textField_1 = new JTextField();
        textField_1.setBounds(90, 38, 300, 21);
        getContentPane().add(textField_1);
        textField_1.setColumns(10);
         
        JButton button_1 = new JButton("\u9009\u62E9\u5730\u5740");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_1_actionPerformed(e);
            }
        });
        button_1.setBounds(400, 39, 93, 23);
        getContentPane().add(button_1);
         
        JButton button_2 = new JButton("\u5F00\u59CB\u590D\u5236");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_copyButton_actionPerformed(e);
            }
        });
        button_2.setBounds(182, 69, 93, 23);
        getContentPane().add(button_2);
    }
    protected void do_button_actionPerformed(ActionEvent e){
        JFileChooser chooser=new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        // 顯示文件打開對話框
        int option = chooser.showOpenDialog(this);
        // 確定用戶按下打開按鈕,而非取消按鈕
        if (option != JFileChooser.APPROVE_OPTION)
            return;
        // 獲取用戶選擇的文件對象
        file = chooser.getSelectedFile();
        // 顯示文件信息到文本框
        textField.setText(file.toString());
    }
    protected void do_button_1_actionPerformed(ActionEvent e){
        JFileChooser chooser=new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int option=chooser.showOpenDialog(this);
        if(option!=JFileChooser.APPROVE_OPTION)
            return;
        file=chooser.getSelectedFile();
        textField_1.setText(file.toString());
    }
  //確定復(fù)制按鈕單擊事件
    protected void do_copyButton_actionPerformed(ActionEvent arg0) {
       Thread thread = new Thread(this);
       thread.start();
    }
   //應(yīng)用多線程技術(shù)實(shí)現(xiàn)讀取操作
    @Override
    public void run() {
        ProgressMonitorTest test = new ProgressMonitorTest();
        String path = textField.getText();
        String save = textField_1.getText();
        test.useProgressMonitor(this,path,save+path.substring(path.lastIndexOf("."),path.length()));
         
    }
}

相關(guān)文章

  • 圖文詳解mybatis+postgresql平臺(tái)搭建步驟

    圖文詳解mybatis+postgresql平臺(tái)搭建步驟

    從頭開始搭建一個(gè)mybatis+postgresql平臺(tái),這篇文章主要介紹了圖文詳解mybatis+postgresql平臺(tái)搭建步驟,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Java簡明解讀代碼塊的應(yīng)用

    Java簡明解讀代碼塊的應(yīng)用

    所謂代碼塊是指用"{}"括起來的一段代碼,根據(jù)其位置和聲明的不同,可以分為普通代碼塊、構(gòu)造塊、靜態(tài)塊、和同步代碼塊。如果在代碼塊前加上 synchronized關(guān)鍵字,則此代碼塊就成為同步代碼塊
    2022-07-07
  • springboot中如何配置LocalDateTime JSON返回時(shí)間戳

    springboot中如何配置LocalDateTime JSON返回時(shí)間戳

    這篇文章主要介紹了springboot中如何配置LocalDateTime JSON返回時(shí)間戳問題。具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • SpringBoot實(shí)現(xiàn)熱部署的三種方式

    SpringBoot實(shí)現(xiàn)熱部署的三種方式

    本文主要介紹了SpringBoot實(shí)現(xiàn)熱部署的三種方式,主要包括配置pom.xml文件,使用插件的執(zhí)行命令mvn spring-boot:run啟動(dòng)項(xiàng),使用springloader本地啟動(dòng)修改jvm參數(shù),使用devtools工具包,感興趣的可以了解一下
    2023-12-12
  • Springmvc應(yīng)用Mongodb分頁實(shí)現(xiàn)

    Springmvc應(yīng)用Mongodb分頁實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Springmvc應(yīng)用Mongodb分頁實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Spring MVC 攔截器實(shí)現(xiàn)代碼

    Spring MVC 攔截器實(shí)現(xiàn)代碼

    本篇文章主要介紹了Spring MVC 攔截器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • 排序算法圖解之Java快速排序的分步刨析

    排序算法圖解之Java快速排序的分步刨析

    快速排序是通過一趟排序?qū)⒁判虻臄?shù)據(jù)分割為獨(dú)立的兩個(gè)部分,一部分的所有數(shù)據(jù)比另外一部分的所有數(shù)據(jù)要小,然后按照此方法對這兩部分分別進(jìn)行快速排序,整個(gè)過程可以遞歸進(jìn)行,以此達(dá)到整個(gè)數(shù)據(jù)變成有序序列。本文通過示例講解了快速排序的實(shí)現(xiàn),需要的可以參考一下
    2022-11-11
  • Java實(shí)現(xiàn)銀行存取款

    Java實(shí)現(xiàn)銀行存取款

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)銀行存取款,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Java語言實(shí)現(xiàn)簡單FTP軟件 輔助功能模塊FTP站點(diǎn)管理實(shí)現(xiàn)(12)

    Java語言實(shí)現(xiàn)簡單FTP軟件 輔助功能模塊FTP站點(diǎn)管理實(shí)現(xiàn)(12)

    這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟,輔助功能模塊FTP站點(diǎn)管理的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • jsp、servlet前后端交互對數(shù)據(jù)處理及展示的簡單實(shí)現(xiàn)

    jsp、servlet前后端交互對數(shù)據(jù)處理及展示的簡單實(shí)現(xiàn)

    Servlet和JSP是Java Web開發(fā)中的兩個(gè)重要概念,在Servlet和JSP中前后端交互可以通過一些方式來實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于jsp、servlet前后端交互對數(shù)據(jù)處理及展示的簡單實(shí)現(xiàn),需要的朋友可以參考下
    2023-12-12

最新評論