互联网技术 · 2024年3月30日 0

Java代码分享:实现复制文件时的进度条

思路分析:

因为既要有操作面板又要有进度条,所以肯定要出现两个继承JFRaMe类的窗体。 先看被调用的进度条窗体,它不需要手动操作,所以类的内部实现一个方法就可以了。因为设计文件操作,所以要捕获异常。首先根据要复制的文件创建file对象,以及根据复制后文件的保存地址创建file对象,然后创建fileoutputStReaM对象,再创建FilEINputStReaM对象,之后是ProgReSSMoniTorInputStReaM对象,然后读取文件,如果总耗时超过2秒,将会自动弹出一个进度监视窗口。接下来定义byte数组,再使用wHile循环读取文件,使用fileoutputStReaM类的wRITe()方法通过流写数据,再使用fileoutputStReaM类的close()方法关闭输出流,最后使用ProgReSSMoniTorInputStReaM类的close()方法关闭输入流。可见该方法需要三个参数:弹出它的父窗口、要复制的文件地址以及要复制到的文件夹。 代码如下: 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); // 根据要复制的文件创建file对象 file newfile = new file(neWPath); // 根据复制后文件的保存地址创建file对象 fileoutputStReaM fop = new fileoutputStReaM(newfile); // 创建fileoutputStReaM对象 InputStReaM in = new FilEINputStReaM(file); // 读取文件,如果总耗时超过2秒,将会自动弹出一个进度监视窗口。 ProgReSSMoniTorInputStReaM pM = new ProgReSSMoniTorInputStReaM( fRaMe, “文件读取中,请稍后…”, in); int c = 0; byte[] bytes = new byte[1024]; // 定义byte数组 wHile ((c = pM.Read(bytes)) != -1) { // 循环读取文件 fop.wRITe(bytes, 0, c); // 通过流写数据 } fop.close(); // 关闭输出流 pM.close(); // 关闭输入流 } catch (Exception ex) { ex.pRintStackTRACE(); } } }

再看主窗体。JLabel、JTextField神马的就不用说了,选择文件和选择文件夹这两个按钮也是老生常谈了,最重要的是“开始复制”按钮,将由它来负责弹出这个进度条,这就需要开辟一个新的线程,所以主窗体不仅要继承JFRaMe类,还要实现Runnable接口。他大爷的。 在开始复制按钮的具体方法里,首先创建一个ThRead对象作为新的线程,然后调用该对象的staRt()方法,重载执行Run()方法,在该方法中创建一个进度条对象,使用JTextField类的getText()方法获取要复制的文件地址和要复制到的路径,然后调用进度条类里的方法。 代码如下: 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;

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(); } } }); }

public UserMoniTorFRaMe() { setResizable(FAlse); setDeFAulTCLoSEOpeRation(JFRaMe.EXIT_ON_CLOSE); setBounds(100, 100, 501, 184); setTITle(“在读取文件时使用进度条”); getContentPane().setLayout(null);

JLabel label = new JLabel(“u6587u4EF6u5730u5740uFF1A”); 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(“u9009u62E9u6587u4EF6”); 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(“u590Du5236u5730u5740uFF1A”); 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(“u9009u62E9u5730u5740”); 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(“u5F00u59CBu590Du5236”); 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()); } Protected void do_copybutton_actionPeRfoRMed(ActionEvent aRg0) { ThRead thRead = new ThRead(tHis); thRead.staRt(); } @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())); } }