Chapter 2 Java Io System

Source: Internet
Author: User
Tags object serialization

For programmers, creating a good input/output system is a difficult task.
1. The file class is actually filepath, which represents the name of a specific file and the name of a group of files under a directory.
Use the list (filenamefilter) method to obtain all the lists contained in this file object. Filenamefilter interface, reload the accept Method
Provides the file name filtering function.

2. The file class not only represents an existing file or directory, but can also be used to create a new directory or a directory path that does not exist with the file object,
View the properties of a file, check whether the file object represents a directory, and delete the file.

3. The I/O class library uses "stream", which represents any data source object that can generate data or an acceptor object that can receive data. I/O
It can be divided into input stream and output stream. The input stream base class is inputstream or reader, and the output stream base class is outputstream or writer. Where
Inputstream and outputstream are the products of Java 1.0 and are byte oriented. Reader and writer are modified by java1.1 I/O.
Provides Unicode and character-oriented I/O functions.

4. Inputstream is used to indicate the input classes generated from different data sources. These data sources include:
1) bytearrayinputstream, A bytearrayinputstream, allows the memory buffer to be used as an inputstream.
2) String object. Stringbufferinputstream.
3) file. Fileinputstream.
4) MPs queue. Pipediputstream
5) a sequence composed of other streams.
6) Other data sources, such as Internet connections.
Filterinputstream is the modifier of inputstream.

5. The outputstream subclass includes bytearrayoutputstream, fileoutputstream, and pipedoutputstream.
, Filteroutputstream (modifier of outputstream)

6. Java I/O streams are combined in a "" manner. The data source is represented by inpustream and outputstream.
Read/write is completed by filterinputstream and filteroutputstream.

7. Filterinputstream: datainputstream, bufferedinputstream, linenumberinputstream,
Pushbackinputstream.
Filteroutputstream: dataoutputstream, printstream, bufferedoutputstream.

8. Reader, writer is the reform level of java1.1, corresponding to inputstream, outputstream, filterinputstream,
Filteroutputstream .. reader and writer have corresponding classes. See the JDK manual. The following classes are not changed:
Dataoutputstream, file, randomaccessfile, sequenceinputstream.
Randomaccessfile is not part of the inputstream or outputstream inheritance system. It is independent.

9. Java standard I/O, system. Out, system. In, system. Err. system class provides some simple static methods,
To allow redirection of standard input, output, and errors:
Setin (inputstream), setout (printstream), seterr (printstream );

10. Java. NiO. * of jdk1.4 introduces a new Java I/O class, which aims to improve the speed and the old I/O has been redesigned using NiO.
Basic Method: Channel and buffer. The only buffer that interacts with the channel is bytebuffer.
Three of the old I/O class libraries have been modified to generate filechannels: fileinputstream and fileoutputstream.
Randomaccessfile. Reader and writer character pattern classes cannot be used to generate channels, but Java. Noi. channels.
The channels class provides a practical method to generate reader and writer in a channel.

11. Example code:
.....
Filechannel fc = new fileoutputstream ("filename. Extend"). getchannel ();
FC. wirte (bytebuffer. Wrap ("some text". getbytes ()));
FC. Close ();
Fc = new randomaccessfile ("data.txt", "RW"). getchannel ();
FC. Position (FC. Size ());
FC. wirte (bytebuffer. Wrap ("some text". getbytes ()));
FC. Close ();
Fc = new fileinputstream ("data.txt"). getchannel ();
Bytebuffer buff = bytebuffer. Allocate (bsize );
FC. Read (buff );
Buff. Flip ();
While (buff. hasremaining ()){
System. Out. Print (char) buff. Get ());
}
 
12. Transferto (), transferfrom () allows one channel to be directly connected with another channel.

13. Byetbuffer. ascharbuffer uses it as charbuffer (provides a good tostring method ). However: Buffer Capacity
It is a common byte. to convert them into characters, either encode them during input or output from the buffer
To decode them. Use the java. NiO. charset. charsetl class.

14. Obtain basic types from bytebuffer, for example:
BB. ascharbuffer (). Put ("some text ");
BB. getchar ();
BB. asw.buffer (). Put (9458 );
BB. getshort ();
...............
Various basic types of packaging can be viewed as the "view buffer" of bytebuffer ";
Bytebuffer BB = bytebuffer. Allocate (size );
Intbuffer IB = BB. asintbuffer (); intbuffer is "view buffer", and so on.

15. Different machines may use different byte sorting methods to store data. Divided into "Big endian" and "little endian ".
Big endian. Java setting method BB. Order (byteorder. big_endian ),
Or byteorder. little_endian

16. Buffer consists of data and four indexes that can efficiently access and operate the data: Mark, position, limit, capacity.

17. The memory ing file allows you to create and modify files that are too large to be stored in the memory.
Mappedbytebuffer out = new randomaccessfile ("t.txt". "WR"). getchannel ().
Map (filechannel. mapmode. read_write, 0, length );

18. Jdk1.4 introduces the file lock mechanism. Lock the ing file.
Fileoutputstream Fos = new fileoutputstream ("file.txt ");
Filelock FL = FOS. getchannel (). trylock ();
If (F1! = NULL)
{
......
F1.release ();
}
FOS. Close ();

19. Java I/O provides the compression function. They are not derived from Reader or writer, but from inputstream and outputstream.
Part of the inheritance system. These classes are: checkedoutputstream, deflateroutputstream
Zipoutputstream, gzipoutputstream and the corresponding inputstream ..

20. Java object serialization converts objects that implement the serializable interface into byte sequences and restores the original classes in the future.
The need for more rigorous persistence mechanisms can be considered using JDO or a tool like hibernate http://hibernate.sourceforge.net
Transient keyword, so that a specific sub-object does not want Java serialization mechanism to automatically save and restore.

21. Jdk1.4 introduces a regular expression. Enhance the string processing capability and replace stringtokenizer.
Basic classes: pattern, pattern, matcher.
More <mastering reular expressions>

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.