Use from Java IO to JDK

Source: Internet
Author: User

One months into the job, the position did not use the familiar C + + language, while working while learning Java. Recently because of the project requirements, often use Java read and write operations, some experience in this collation, the main idea: first introduce the basic IO function, and then how to choose the specific IO operation according to the function needs.

First, Java IO Common class diagram and its basic functions:

View the Java JDK to discover the following 8 base classes are included in the IO package: (Image source: http://www.falkhausen.de/Java-8/java.io/index.html)

Figure 1 JAVA.IO Package Structure

which

1 InputStream and OutputStream are the two core classes of IO packets (input and output streams), which can be thought of as data streams with some properties that connect disks and programs: File <---> Flow <---> programs.

Figure 2 Concept diagram of the stream

The 2 file is primarily used to create a Document object (read, write), which is a construction parameter of the input stream and output stream, which tells the path of the stream to read and output files.

3 Reader and writer classes provide a way to read and write streams, connecting byte stream and character stream, and buffer streams. You can either read the data from the stream or write data to the stream.

4 Objectstreams and Misc are used for the serialization of Stream objects, exceptions for exception handling.

The following is a UML class diagram of InputStream and OutputStream, these two graphs may not be very clear, you can refer to http://www.falkhausen.de/Java-8/java.io/index.html , this is a display Java JDK Class Diagram Web site, for the summary of knowledge points is still very good

Figure 3 InputStream

Figure 4 OutputStream

Derive different subclasses from two large base classes, each of which is responsible for one type of input or output, synthesizes the main sub-class characteristics, summarizes its use characteristics as follows:

II. Select IO Operations according to project requirements:

The choice and use of Io, first of all to clear the use of occasions, requirements. The recent work encountered a compressed stream problem, also belongs to the IO stream. Requirements are in the decompression and compression of the disk (large data volume), you need to build a method, input parameters for the zip stream (zipinputstream), the data stream parsing, the object's file generation, the new file is written to the zip stream. Sort out the problem-solving routines:

1 Zipinputstream is a compressed input stream whose original base class is InputStream, which can be extracted with this stream object, which is no problem.

2 How are the results of the analysis data added to the zip stream? Naturally think of it with its brother Zipoutputstream, the data compression, and reference Zipoutputstream object construction method for ZipOutputStream(OutputStream out) , it is common to create a compressed file object output stream (online tutorial basically is such an example, You need to specify a compressed output file. zip), but this does not meet the requirements

3 views OutputStream The structure of this base class, such as 4, whose subclasses contain five FileOutputStream Bytearrayoutputstream Fileteroutputstream, Where FileOutputStream is a common stream object, you need to specify the output file of the stream FileOutputStream (file file), whereas the view Bytearrayoutputstream is instantiated byByteArrayOutputStream()

Creates a new byte array output stream. This shows that the output stream object is to keep the data in an array of memory instead of the specified disk file, which can be used. So there is the following validation code: compressing a data stream into an array of memory

 Public Static byte[] Zip (byte[] data) {        byte[] B =NULL; Try{bytearrayoutputstream Bos=NewBytearrayoutputstream (); Zipoutputstream Zip=NewZipoutputstream (BOS); ZipEntry entry=NewZipEntry ("Zip");            Entry.setsize (data.length);            Zip.putnextentry (entry);            Zip.write (data);            Zip.closeentry ();            Zip.close (); b=Bos.tobytearray ();        Bos.close (); } Catch(Exception ex) {ex.printstacktrace (); }        returnb; }

4 is the whole process of the method collation, input parameters for the Zipinputstream data stream, the decompression analysis, the results are written into a byte array, and then use the Zipoutputstream stream object to compress the array, generating a compressed byte array

5 Convert the compressed array to a new Zipinputstream (the conversion process is similar to the above analysis process, using the InputStream subclass Bytearrayinputstream)

Newnew Zipinputstream (bis);

Iii. Summary

Because the first contact with Java, and a lot of people, in the Internet to find a variety of samples, only know leaf out to write, in fact, the construction of each object, the conversion is not very understanding, the results of a long time, have not found a good method. Even if you finally find a solution, you waste a lot of time and energy. So it took two days to sort out the following Java IO structure and some use methods, so that we could spread the

At least later know how to use Java JDK, encounter problems how to find, temporarily to here, and later encountered new IO method to add.

Use from Java IO to JDK

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.