1.JAVA Basic--io

Source: Internet
Author: User

1. The concept of flow

An abstract concept of input, the operation of files in Java is done in a "flow" manner. The stream can be understood as a "pipe", and the data is transferred from one end to the other through the pipe.

Flow has directionality: In general, this direction is based on the program as a reference, so, the input stream refers to the flow of the program, the output stream refers to the flow from the program output, it can also be understood that the program from the input stream to get data, write data to the output stream .

Streaming input and output is characterized by: data acquisition and transmission in the order of the data sequence, that is, each data must wait for the data in front of it, and so on before the data read in or sent out before being read and written. As a result, streams and queues can only read and write to the data in "FIFO" mode, rather than choosing a read-write location.

Design principles for 2.JAVA IO libraries

Two principles

1. Input and output symmetry. For example: InputStream and OutputStream each occupy the root of two parallel hierarchies of input and output of the byte stream, and reader and writer correspond to the roots of the two parallel hierarchies of character stream input and output.

The symmetry of 2.byte and Char. such as: Inputsteam and Reader,outputsteam and writer.

Two design modes

1. Decorator mode (Decorator). Decorators, also known as wrappers, act like subclasses to implement a parent class and customize more and richer methods, but decorator mode is more flexible than subclasses.

Taking InputStream as an example, this paper describes the embodiment of the decorator pattern. Here is a simple understanding of the individual: InputStream as the abstract component root node, FileInputStream, StringBufferInputStream, The Bytearrayinputstream is the concrete realization of the adornment object, while the FilterInputStream and its subclasses are the decorators. Since all of the elements are descendants of the component root node, they are consistent on the method invocation, and the decorator is able to add its own logic to the original method by invoking the decorated object during the build process, so that the concrete method can be implemented by adding its own logical before the decorator concrete method. (Can retrieve "decorator mode")

2. Adapter mode (Adapter). Take FileInputStream as an example.

 Public classFileInputStreamextendsinputstream{/*file Descriptor-handle to the open File*/    PrivateFileDescriptor FD; PublicFileInputStream (FileDescriptor fdobj) {SecurityManager security=System.getsecuritymanager (); if(Fdobj = =NULL) {        Throw NewNullPointerException (); }    if(Security! =NULL) {security.checkread (fdobj); } FD=Fdobj; }......

First understand the Java disk IO Implementation mechanism: The file is the operating system and disk drive interaction of the smallest unit, the program can only through the file to manipulate the data on the disk. In Java, a file operation is done through the files class, but a file class does not actually represent a real-world object, and when new is a class, it returns only the virtual object associated with the path described by the typeface, and the object is actually manipulated. is implemented by the underlying system interaction through the filesystem attribute referenced in the file class (which, of course, is not accessible to the underlying operating system by Java itself, but only through the JNI interface to other languages for the underlying access).

The above analysis shows that, in the same way, FileInputStream in invoking system files, the implementation of the adapter pattern can be understood: by referencing the FileDescriptor class, and to adapt it to the Inputsteam type of object, implement the file operation input stream.

3. Classification of streams

per unit of data processed: byte stream, character stream

Points by data direction (relative to program itself): input stream, output stream

By function: node stream, process flow

The 3rd explanation is that a node stream is a stream that reads and writes data from a particular data source, whereas a process flow is based on an existing stream (either a node stream or a processing stream), a stream that provides more powerful functionality for data processing, also known as a "filter flow," which is a wrapper over the original stream.

Four basic abstract classes: input stream--inputstream, Reader; output stream--outputstream, Writer

Implementation class, Dark color for node flow, light for processing flow:

4.read and Write methods

    • In Inputstream:java, the InputStream class defines three read methods:
      • public abstract int Read () throws IOException; reads the next byte of data from the input stream, returning the read byte value. The return value is an int value of 0-255 and returns 1 if the end of the stream is reached.
      • public int read (byte b[]) throws IOException; reads B.length bytes of data from the input stream and stores it in buffer data B, returning the actual number of bytes read.
      • public int read (byte b[], int off, int len) throws IOException; reads Len bytes of data from the input stream and writes from the off position of B to B.
    • OutputStream: Same as three write methods:
      • public abstract void Write (int b) throws IOException; writes the specified byte (b) to this output stream, in fact the 8 low-order bits of B that are written here, and 24 of its Gauchebutte will be ignored.
      • public void Write (byte b[]) throws IOException writes a b.length byte from the specified byte array (b) to this output stream.
      • public void Write (byte b[], int off, int len) throws IOException; writes Len bytes from off in the specified array B to this output stream.

5. Byte stream and character stream implementation

① byte-based input stream :

Notable places: 1. The node stream (LEVEL2) interacts directly with the data source, so it mostly indicates the form of the data source: ByteArray, File, Piped;2, The filter stream (LEVEL3) is a wrapper based on other streams (all integrated from FilterInputStream), which provides richer functionality, so the filter flow is mostly a function: such as buffered, linenumber.

Level2:

    • Bytearrayinputstream: Reads a byte of data from memory , when its instance is created, a buffer of byte array type is created inside the program, and the read data is saved in the buffer. Use a counter to record the number of bytes read from the data source. Note that in the source code, the close () method of the Bytearrayinputstream is empty and does not have any practical effect, because it is read from memory, there is no argument to close the connection.
    • FileInputStream: Reads a single byte of data from a file . FileInputStream the file operation class FileDescriptor to InputStream, which enables the file to be read.
    • PipedInputStream: The pipeline input stream can be connected to the pipeline output flow, which reads a byte of data from the pipeline output stream , mainly for multithreading. The input stream and output stream are divided into two threads to avoid thread deadlock.
    • Sequenceinputstream: Merges multiple input streams sequentially into one input stream.

LEVEL3:

    • DataInputStream: The data stream is used to manipulate the underlying type of data, converting the obtained data flow to the original data type.
    • Bufferedinputstream: The buffer stream provides an internal buffer for reading data so that when reading the data, more data can be read in the buffer and then returned. With an internal buffer, Bufferedinputstream provides a more optimized way to read data and reduces IO operations. In addition, Bufferedinputstream provides the mark (notation method), reset (reset Tag method), skip (skips the specified bytes), available (estimated number of available bytes methods).

② byte-based output stream:

Level2:

    • PrintStream: Byte print stream, which provides a series of print and println methods that can be output to the underlying output stream into data that is converted to a string. The print stream does not throw an IO exception, and an exception will set its Trouble property to true, and the consumer can call the CheckError method to check for an exception. Also, the PrintStream self-flush function automatically executes flush emptying the cache after its output method is called.
    • Dataoutputsteam: Similar to DataInputStream, it can write basic type data in memory directly to the underlying output stream.
    • Bufferedoutputstream: Provides buffering for the output stream, and all output requests are buffered in the Bufferedoutputstream buffer, and then uniformly written out at the right time. So its write method might cache the data first and call the Flush () method if it needs to be written immediately.

LEVEL3:

    • Bytearrayoutputstream: The output stream is used to output data to memory, the data is cached in its own buffer before the output, and its Close method is empty because it is writing data to memory, there is no link to close the argument.
    • FileOutputStream: Used to output bytes of data to a file. Under some systems, the same file allows only one output stream to be opened.

③ character-based input stream:

Note: Virtually all IO operations on files are implemented in bytes, and the so-called character stream is just a logical statement.

The above class does not introduce too much, basically corresponds to the byte input stream, where StringReader reads from a string, and LineNumberReader is a character input stream that can track the read-in "row data," which contains an indicator that tracks the number of rows that are read into the data.

④ character-based output streams:

⑤ conversion between a byte stream and a character stream:

InputStreamReader and OutputStreamWriter provide the conversion of the byte input stream to the character input stream and the character output stream to the byte output stream respectively, and the conversion can specify the encoding.

6.RandomAccessFile

Randomaccessfile is an independent implementation of the Javaio operation class, which is not in the input and output stream structure described above. As a random file stream that expands the IO framework, it can read or write data anywhere in the file. It creates a file pointer in the file system that marks the next byte of the read-write operation, and the Seek method can move the pointer anywhere inside the file to allow random reads of the file.

7. Instance Code

Code Listing 1: Copying a page (resource) on the network to local (page static)

 Public Static voidMain (string[] args)throwsException {InputStream in=NewBufferedinputstream (NewURL ("http://www.jd.com"). OpenStream ()); File Bdfile=NewFile ("d:\\jd.html"); if(!bdfile.exists ())    {Bdfile.createnewfile (); } outputstream ou=NewBufferedoutputstream (NewFileOutputStream (bdfile)); byte[] B =New byte[1024]; intLen;  while(len = in.read (b, 0, b.length))! =-1) {Ou.write (b,0, Len); }}

Code Listing 2: Downloading a network resource through multithreading to local (using the Randomaccessfile Class)

 Public classMain {/**     * @paramargs*/     Public Static voidMain (string[] args)throwsexception{String Path= "XXX";//network resource URL String filePath= "d:\\";//Local Disk locationNewMain (). Threaddownload (Path, 5, FilePath); }     Public voidThreaddownload (String Path,intThreadsize, String FilePath)throwsexception{URL url=NewURL (path); HttpURLConnection HttpURLConnection=(HttpURLConnection) url.openconnection (); Httpurlconnection.setrequestmethod ("GET"); if(Httpurlconnection.getresponsecode ()! = 200) {            Throw NewException ("Response exception"); }                intDatalength =httpurlconnection.getcontentlength (); String FileName=Main.getfilename (httpurlconnection);        Httpurlconnection.disconnect (); Threadsize= Threadsize = = 0? 5: threadsize; intBlockSize = datalength/threadsize; intleft = datalength%threadsize;  for(inti = 0; i < threadsize; i++) {            if(i = =threadsize) {BlockSize+=Left ; }            NewThread (NewDownloadthread (Path,NewRandomaccessfile (NewFile (FilePath + fileName), "RW"), blockSize * I, blockSize, i + 1) . Start (); }    }    Private StaticString GetFileName (httpurlconnection httpurlconnection)throwsexception{Map<string, list<string>> headerfields =Httpurlconnection.getheaderfields (); Set<entry<string, list<string>>> set =Headerfields.entryset ();  for(Entry<string, list<string>>Entry:set) {            if(Entry.tostring (). Contains ("FileName") ) {List<String> list =Entry.getvalue ();  for(String string:list) {if(String.contains ("filename") ) {String Filenameresult=NewString (String.getbytes (), "UTF-8"); returnFilenameresult.substring (Filenameresult.indexof ("\" ") + 1, filenameresult.lastindexof (" \ "")); }                }            }        }        return NULL; }} Public classDownloadthreadImplementsrunnable{PrivateString Path; PrivateRandomaccessfile Randomaccessfile; Private intstartsize; Private intblockSize; Private intThreadName;  Publicdownloadthread (String path, Randomaccessfile randomaccessfile,intStartsize,intBlockSize,intthreadname) {         This. Path =path;  This. Randomaccessfile =Randomaccessfile;  This. startsize =startsize;  This. blockSize =blockSize;  This. ThreadName =ThreadName; } @Override Public voidrun () {httpurlconnection httpurlconnection=NULL; InputStream InputStream=NULL; Try{System.out.println ("Thread" +threadname+ "Start Download"); URL URL=NewURL (path); HttpURLConnection=(HttpURLConnection) url.openconnection (); Httpurlconnection.setrequestmethod ("GET"); Httpurlconnection.setreadtimeout (10 * 1000); Httpurlconnection.setrequestproperty ("Range", "bytes=" + startsize + "-"); if(Httpurlconnection.getresponsecode ()! = 206{//206 Partial Content customer sends a GET request with a range header, the server finishes it (HTTP 1.1 new) System.out.println ("Thread" + threadname + "Response exception, code =" +Httpurlconnection.getresponsecode ()); Throw NewException ("Response exception"); } InputStream=Httpurlconnection.getinputstream (); byte[] B =New byte[1024]; intLen =-1; intLength = 0;            Randomaccessfile.seek (startsize);  while((len = Inputstream.read (b))! =-1) && Length <blockSize) {Randomaccessfile.write (b,0, Len); Length+=Len;            } randomaccessfile.close ();            Inputstream.close ();            Httpurlconnection.disconnect (); System.out.println ("Thread" +threadname+ "Download Complete"); } Catch(Exception e) {e.printstacktrace (); } finally{randomaccessfile=NULL; InputStream=NULL; HttpURLConnection=NULL; }    }}

1.JAVA Basic--io

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.