Use NIO to make underlying copies of files (in bytes) multithreading technology primary applications do not block programs from running
Package Com.lovo.homework01;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.nio.bytebuffer;import Java.nio.channels.filechannel;import Javax.swing.JButton; Import Javax.swing.jframe;import javax.swing.jprogressbar;import javax.swing.jtextfield;/** * Class: File copy * @author Abe * NIO application, JProgressBar application */@SuppressWarnings ("Serial") public class Niotest extends JFrame {private JProgressBar jbar = null; private int totlesize = 0;private int copyedsize = 0;private int eachsize = 0;private JButton Startbutton = null;private J TextField infield = null, outfield = null;/** * Constructor */public niotest () {this.setsize (+); this.setresizable (false); th Is.setdefaultcloseoperation (Exit_on_close); This.setlocationrelativeto (null); This.setlayout (null);/** * JProgressBar Settings */jbar = new JProgressBar () jbar.setminimum (0); jbar.setmaximum (100);//display upper and lower bounds Jbar.setvAlue (0);//Initial value jbar.setstringpainted (TRUE);//Display percent jbar.setbounds (50, 150, 300, 30);/** * text input box */infield = new JTextField (); Infield.setbounds ((); outfield = new JTextField (); Outfield.setbounds (50, 100, 300, 30);/** * Copy Start button */star TButton = new JButton ("Start copy"); Startbutton.setbounds (150, 200, 100, 30);/** * button plus listener, anonymous inner class in-place instantiation */ Startbutton.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) { Startbutton.setenabled (FALSE);/** * Multithreading technology Create a new thread-in-place instantiation Runnable Interface */new thread (new Runnable () {@Overridepublic void run () {//New input/output stream here filexxxxxx is because of the NIO method that they can use fileinputstream in = null; FileOutputStream out = null;try {in = new FileInputStream (Infield.gettext ()), out = new FileOutputStream (outfield.gettext ());//nio exclusive transmission channel FileChannel fcin = In.getchannel (); FileChannel fout = Out.getchannel (); totlesize = In.available ();//nio Exclusive Transport trolley Bytebuffer buffer = bytebuffer.allocate (4096 ); while ((Eachsize = fcin.read (buffer))! =-1) {//pointer back to 0 and start reading data until the end of the content Buffer.flIP (); fout.write (buffer);//Empty Trolley buffer.clear ();//Set progress bar display content Copyedsize + = Eachsize;jbar.setvalue ((int) (100.0 * copyedsize/totlesize));}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {// Close input and output stream if (in! = null) {try {in.close ()} catch (IOException e) {e.printstacktrace ()}} if (out! = null) {try {out.close ();} catch (IOException e) {e.printstacktrace ();}}}}). Start ();//Do not forget the new thread started running with this}); This.add (infield); This.add (outfield); This.add (Startbutton); This.add (Jbar);} /** * Main Method Set window visible * @param args */public static void main (string[] args) {new Niotest (). setvisible (True);}}
Java programming ()-----Making file copy software process input stream output stream NIO progress bar Bottom Copy multithreading