File copy of Io stream in Java

Source: Internet
Author: User
Tags file copy

O (∩_∩) o haha ~

1. Overview

A mature language must have several modules: IO, Communication, threading, UI ...

Java as a mature programming language, its IO stream is more complex. Last picture everyone feels the following:

For a simple analysis, Io is divided into two streams: a character stream and a stream of bytes. The character stream of the parent reader (read into memory) and writer (from memory output), byte stream parent class InputStream (read into memory) and OutputStream (from memory output), and then for the convenience of various operations, such as for file operations, derived from the file stream ; object flow is derived for object manipulation, and so on. At the beginning I was also silly to know whether it is input or output, in fact, understand who is the subject is easy to understand, with you are now writing the program as the main body, input is flow into your program, output is from your program outflow.

2. Understanding of Buffering

When I first started learning Io, I didn't understand what the buffered really did, why do I have to have one-step buffer transition to input and output? Later still read Baidu know the last big God wrote, personally feel very reasonable, copy paste as follows, should not count infringement bar (⊙﹏⊙)

"If it is read and write, it will be very slow, also hurt the hard disk." Buffer is an area in memory, the data is stored in memory, and then write once, similar to the batch operation of the database, so that the efficiency is higher.

when invoking the i\o operation, it is actually a read or write, the key is that the CPU has only one, no matter how many cores. Will the CPU be involved in the main operation when the system is called? It takes more time to take part in many times.

when system calls are not buffered, the CPU will consider using interrupts as appropriate. At this time the CPU is active, each cycle to spend a part to ask whether the i\o device read the data, the CPU can not do any other things (at least responsible for the implementation of this module of the kernel is not). So, the call reads one word at a time, informs once, and the CPU frees up time to process it once.

when Buffering is set, the CPU usually uses DMA to perform i\o operations. The CPU gives this work to the DMA controller to make time to do other things, and when the DMA is done, the DMA will actively tell the CPU to "do it". At this point, the CPU takes over the subsequent work. In this case, the CPU is passive. DMA is dedicated to i\o and memory data exchange, not only its own high efficiency, but also save the CPU time, the CPU at the beginning and end of the DMA to do some settings.
So, call once, do not have to notify the CPU, and so the buffer is full, DMA will say to C PU "Hey, man!" Come and see, move them all. "

In conclusion, the buffer is set up and the data block is built, making the DMA execution more convenient and the CPU idle, rather than waiting for the i\o data to read. From a micro point of view, setting the buffer efficiency is much higher. Although, it cannot be seen from this program. You can see the gap by reading and writing tens of thousands of words. "

OK, from the above can be seen, save time and effort.

3. File copy

The principle of file copy is very simple, from the hard disk to read the file stream to the program, and then from the program output stream to the target file, the file is completed the copy. In fact, the file input and output stream in Java is used. Look at the code is very much, in fact, ignore those try-catch and exception handling, very simple lines of code.

1 Package test0713;2 3Import java.io.*;4 5  Public classManage {6     PrivateInputStream input;7     Privateoutputstream output;8     Private Static intlength;9 /**Ten * Copy of File One      *  A * @param beginfilename - * Original File - * @param endfilename the * Target file -      */ -      Public voidfileCopy (String beginfilename, String endfilename) { -         //creating an input-output stream object +         Try { -input =NewFileInputStream (beginfilename); +Output =NewFileOutputStream (endfilename); A  at             //Get file length -             Try { -Length =input.available (); -  -                 //Create a cache area -                 byte[] buffer =New byte[length]; in                 //writing data from a file to a cache array - input.read (buffer); to                 //output data from a cached array to a file + output.write (buffer); -  the}Catch(IOException e) { *  $ e.printstacktrace ();Panax Notoginseng             } -  the}Catch(FileNotFoundException e) { + e.printstacktrace (); A  the}finally { +             if(Input! =NULL&& Output! =NULL) { -                 Try { $Input.close ();//Close the stream $ output.close (); -}Catch(IOException e) { - e.printstacktrace (); the                 } -             }Wuyi  the         } -     } Wu  -      Public Static voidMain (string[] args) { AboutManage man =NewManage (); $         //The first parameter is the file that will be copied, the second parameter is the target file for the output (if not, create a new one, implement the file copy) -Man.filecopy ("F:\\codeworkspace\\java\\filecopy\\src\\test0713\\manage.java", -                 "C:\\users\\yuxiu\\desktop\\123.txt"); -  A     } +  the}

File copy of Io stream in Java

Related Article

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.