南通颐猩文化传播有限公司

當(dāng)前位置:首頁(yè) >  站長(zhǎng) >  編程技術(shù) >  正文

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

 2021-01-07 16:46  來(lái)源: 網(wǎng)絡(luò)綜合   我來(lái)投稿 撤稿糾錯(cuò)

  域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)

思路分析:

因?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對(duì)象,以及根據(jù)復(fù)制后文件的保存地址創(chuàng)建File對(duì)象,然后創(chuàng)建FileOutputStream對(duì)象,再創(chuàng)建FileInputStream對(duì)象,之后是ProgressMonitorInputStream對(duì)象,然后讀取文件,如果總耗時(shí)超過(guò)2秒,將會(huì)自動(dòng)彈出一個(gè)進(jìn)度監(jiān)視窗口。接下來(lái)定義byte數(shù)組,再使用while循環(huán)讀取文件,使用FileOutputStream類的write()方法通過(guò)流寫(xiě)數(shù)據(jù),再使用FileOutputStream類的close()方法關(guān)閉輸出流,最后使用ProgressMonitorInputStream類的close()方法關(guān)閉輸入流。可見(jiàn)該方法需要三個(gè)參數(shù):彈出它的父窗口、要復(fù)制的文件地址以及要復(fù)制到的文件夾。

代碼如下:

ProgressMonitorTest.java:
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對(duì)象
            File newFile = new File(newPath); // 根據(jù)復(fù)制后文件的保存地址創(chuàng)建File對(duì)象
            FileOutputStream fop = new FileOutputStream(newFile); // 創(chuàng)建FileOutputStream對(duì)象
            InputStream in = new FileInputStream(file);
            // 讀取文件,如果總耗時(shí)超過(guò)2秒,將會(huì)自動(dòng)彈出一個(gè)進(jìn)度監(jiān)視窗口。
            ProgressMonitorInputStream pm = new ProgressMonitorInputStream(
                    frame, "文件讀取中,請(qǐng)稍后...", in);
            int c = 0;
            byte[] bytes = new byte[1024]; // 定義byte數(shù)組
            while ((c = pm.read(bytes)) != -1) { // 循環(huán)讀取文件
                fop.write(bytes, 0, c); // 通過(guò)流寫(xiě)數(shù)據(jù)
            }
            fop.close(); // 關(guān)閉輸出流
            pm.close(); // 關(guān)閉輸入流
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

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

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

代碼如下:
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);
        // 顯示文件打開(kāi)對(duì)話框
        int option = chooser.showOpenDialog(this);
        // 確定用戶按下打開(kāi)按鈕,而非取消按鈕
        if (option != JFileChooser.APPROVE_OPTION)
            return;
        // 獲取用戶選擇的文件對(duì)象
        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()));

    }
}

申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)標(biāo)簽
java進(jìn)度條

相關(guān)文章

熱門(mén)排行

信息推薦