It may take some time to operate large files. It is very common to provide progress bar prompts for users, so that users can understand the time required for file operations. This example introduces the progress bar prompt when copying large files. Note that only when the file read exceeds
Train of Thought Analysis:
The Code is as follows:
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 ); // create the File object File newFile = new File (newPath) based on the File to be copied; // create the File object FileOutputStream fop Based on the SAVE address of the copied File = New FileOutputStream (newFile); // create the FileOutputStream object InputStream in = new FileInputStream (file); // read the file. If it takes more than 2 seconds, A progress monitoring window is displayed automatically. ProgressMonitorInputStream pm = new ProgressMonitorInputStream (frame, "reading the file... ", in); int c = 0; byte [] bytes = new byte [1024]; // defines the byte array while (c = pm. read (bytes ))! =-1) {// cyclically read the file fop. write (bytes, 0, c); // write data through stream} fop. close (); // close the output stream pm. close (); // close the input stream} catch (Exception ex) {ex. printStackTrace ();}}}
3. Check the main form again. Runnable interface. His uncle.
4. in the specific method of the Start copy button, first create a progress bar object in this method, and use the getText () method to obtain the file address and path to be copied, then, call the method in the progress bar class.
The Code is as follows:
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. fileWrit Er; 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 {/*****/priv Ate static final long serialVersionUID = response; private JPanel contentPane; private JTextField fileField; private JTextField searchTextField; private JTextField replaceTextField; 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); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setBounds (100,100,501,184); setTitle ("use progress bar when reading files"); getContentPane (). setLayout (null); JLabel label = new JLabel ("\ u6587 \ u4EF6 \ u57 30 \ 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 actionreceivmed (ActionEvent e) {do_button_actionreceivmed (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.addActionL Istener (new ActionListener () {public void actionreceivmed (ActionEvent e) {do_button_?action=med (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 actionreceivmed (ActionEvent e) {do_copybutton_actionreceivmed (e) ;}}); button_2.setBounds (182, 69, 93, 23); getContentPane (). add (button_2);} protected void do_button_actionreceivmed (ActionEvent e) {JFileChooser chooser = new JFileChooser (); chooser. setFileSelectionMode (JFileChooser. FILES_ONLY); // display the file opening dialog box int option = chooser. showOpenDialog (this); // confirm that the user presses the OPEN button instead of the cancel button if (option! = JFileChooser. APPROVE_OPTION) return; // obtain the selected file object file = chooser. getSelectedFile (); // display the file information to the textField in the text box. setText (file. toString ();} protected void do_button_?action=med (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 ();} // click the event protected void do_copybutton_actionreceivmed (ActionEvent arg0) {Thread thread = new Thread (this); thread. start () ;}// read by applying multithreading technology @ 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 ()));}}
Effect