JAVA programming (17) ----- prepare file copy software process input stream output stream NIO progress bar bottom copy Multithreading

Source: Internet
Author: User

JAVA programming (17) ----- prepare file copy software process input stream output stream NIO progress bar bottom copy Multithreading

Using NIO to copy files at the underlying layer (byte) multi-threading technology elementary applications run programs without blocking

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. JTextFi Eld;/*** 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 JTextField inField = null, outField = null;/*** constructor */public NIOTest () {this. setSize (400,300); this. setResizable (false); this. Setdefaclocloseoperation (EXIT_ON_CLOSE); this. setLocationRelativeTo (null); this. setLayout (null);/*** JProgressBar setting */jbar = new JProgressBar (); jbar. setMinimum (0); jbar. setMaximum (100); // display the upper and lower limit jbar. setValue (0); // The Initial Value jbar. setStringPainted (true); // display the percentage of jbar. setBounds (50,150,300, 30);/*** text input box */inField = new JTextField (); inField. setBounds (50, 50,300, 30); outField = new JTextField (); outField. setBoun Ds (50,100,300, 30);/*** copy start button */startButton = new JButton ("START copy"); startButton. setBounds (150,200,100, 30);/*** Add the listener button, and the anonymous internal class is instantiated locally */startButton. addActionListener (new ActionListener () {@ Overridepublic void actionreceivmed (ActionEvent e) {startButton. setEnabled (false);/*** multithreading technology creates a Thread to instantiate the Runnable interface */new Thread (new Runnable () {@ Overridepublic void run () {// create an input/output stream. Filexxxxxx is used here because they are using NIO. 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 vehicle ByteBuffer buffer = ByteBuffer. allocate (4096); while (eachSize = fcin. read (buffer ))! =-1) {// the pointer returns to zero and starts to read data until the end of the content buffer. flip (); fout. write (buffer); // clear the car buffer. clear (); // set copyedSize + = eachSize; jbar. setValue (int) (100.0 * copyedSize/totleSize) ;}} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {// close the input and output streams 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 to use this to start running the new thread}); this. add (inField); this. add (outField); this. add (startButton); this. add (jbar);}/*** the main method setting window is visible * @ param args */public static void main (String [] args) {new NIOTest (). setVisible (true );}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.