20155335 Eucun "Java Programming" Eighth Week summary

Source: Internet
Author: User
Tags rewind

2016-2017-2 "Java Programming" X-week study summary

# #认识NIO

In Java, input and output, basically the low-level processing in bytes, in fact, most of the byte array in the entire chunk processing, for the dump () method, must handle byte[], must specify the starting point and the number of bytes written out of byte[]. For example

Java.. There are some decorative classes in the IO suite, such as Datainputstream,dataoutputstream,bufferedreader and BufferedWriter, if you want to tag a chunk of data that is interesting in a byte or string, like a tag read location , data valid locations, for these block tags, provides clear (), rewind (), flip (), compact () and other advanced operations, for the above dump () method, if using NIO, you can write as follows:

At this stage, you can understand that in the program demonstration, just confirm that the data is read from the channel into buffer (the read () method does not return-1), using the Advanced Flip () method to mark the read-in buffer in buffer to write to another channel, Finally, use the clear () method to clear the buffer tag, and in this process, do not touch byte[] related details.

Channel Architecture and operations

The related interfaces and classes of channel in Noi, is located in the Java.nio.channle suite, the channel interface is the sub-interface of the autocloseable, so you can use the attempt to close the resource syntax, the channel interface is the main new IsOpen () method.

Readablebytechannel defines the read () method, which is responsible for reading the data in Readablebytechannel to Bytebuffer,writeablebytechannel defines the write () method, Responsible for writing Bytebuffer data into Writeablebytechannel, Scatteringbytechannel defines the read () method, It can be said that Scatteringbytechannel is assigned to the Bytebuffer array, The write () method defined by Gatheringbytechannel can write data for the Bytebuffer array to Gatheringbytechannel.

Writeablebytechannel behavior, the Bytechannel Word interface Seekablebytechannel can be read to change the location of the next store to be read.

Schema and operation of buffer

In the NOI design, the data is in Java. In Nio.buffer, buffer is an abstract class that defines advanced operations on data chunks such as clear (), flip (), Reset (), rewind (), which are all buffer and actually return this, As a result, pipeline operation Styles can be formed when continuous advanced operations are required.

Depending on the type of data processing needs, you can choose different · Buffer subclass, they are abstract classes, and therefore can not be directly instantiated, however, the direct subclass of Buffer has a allocate () static method, so that the specified buffe capacity, if it is bytebuffer, the capacity refers to internal operation using the byte[] length, If it is floatbuffer, the capacity refers to the float[] length, and so on, the buffer capacity size can be obtained using the capacity () method, if you want to use an array inside buffer, you can use the method of array (), If there is one array that you want to convert to a buffer subclass instance, each buffer () subclass instance has a warp () static method to provide this service.

If you use Bytebuffer, there is also a allocatedirect () method, the memory configured with respect to the allocate () method has JVM management, and Allocatedirect () takes advantage of the operating system's native I/O operations, trying to avoid mediation transfers from the JVM, In theory, it is more efficient than allocate () configuration memory, but Allocatedirect () will consume more system resources when configuring memory, so it is recommended to use only large, long-stocked bytebuffer objects, and to observe the occasions except for obvious functional differences. Learned by Isdirect ().

Buffer is a container in which the filled data does not exceed his capacity, and the value of the data boundary index that is actually read or written can be learned or qualified by the limit () method.

The bufffer operation can begin with clear (), flip (), and Rewind (), when a buffer has just been configured or called the Clear method, limit () equals capacity (), position () will be 0.

Here is a complete example of using Flip ()

There is also a mark () method on buffer, which can be marked on the current position, after the buffer is accessed, if the Reset () method is called, the position will be set back to the location marked by the mark (). Between position and limit is the remaining accessible data, you can use the remaining () method to know how much length, using hasreamming () can test whether there is still the remaining data to be accessed.

Architecture of the NIO2

Application developers primarily use Java.nio.flie and Java.nio.file.attribute, an abstract class or interface that must be manipulated in a package, operated by a file system provider, and the application does not have to worry about how the underlying system actually accesses the filesystem. Usually only the file system needs to care about the JAVA.NIO.FILE.SPI package.

The center of the NIO2 file system is Java.nio.file.spi.FileSystemProvider, itself is an abstract class, which is a class that the file system provider can manipulate in order to produce the operands of various abstract classes or interfaces in Java.nio.file and Java.nio.file.attribute.

For learners, as long as they know the existence of filesystemprovider, we can get related operation objects or perform various file system operation through the static methods provided by filesystem, paths, files and other classes in the Java.nio.file package. , these static methods internally use Filesystemprovider to get the desired operand and complete the operation.

To manipulate the document, first point to the document path. The path instance is the representative object of the path in the JVM and the starting point for the NIO2 file system API operation, and there are many operations in the NIO2 file system API that must be specified using path.

To get the path instance, you can use the Paths.get method, the most basic way, is to use a string path, you can use a relative path or an absolute path, for example:

Path workspace = Paths.get ("C:\\workspace");

Path books = Paths.get ("Desktop\\books");

Path Path = Path.get (Args[0]);

The second parameter of Paths.get () begins to accept an indefinite length of argument, so you can specify the path after which the path fragment is specified. For example, the following specifies the documents\downloads in the user directory:

The paths instance represents only the path information, and the path actually corresponds to a document or folder that does not necessarily exist. Path provides a number of ways to obtain various information about the path, such as:

The path element is in folders and the topmost index is 0.

Paths operates the Iterable interface, you can use the enhanced for loop syntax or the Forbath () method to step through the path information of the segment in path, for example:

Path.path = Path.get (System.getproperty ("Users.home"), "Documents", "documents");

Path.foreach (out::p rintln);

There is redundant information in the path and you can use Normorlize.

Read access Directory

If you want to get the file system root path information, you can use the Filesyatem getrootdirectories () method, which returns the Iterabe<string> object. You can use an enhanced for or loop or a new foreach () party to obtain the path information for the root directory, for example:

You can also use the Files.newdirectorystream () method to get an Directorystream interface operand that represents all the documents under the path.

This example allows you to specify a directory path from a command-line argument and query the document in that directory:


Summarize what you learned this week as simple as possible.

Try not to transcription, waste time

Read it, do not understand, learn to remember the experience

# # [Code Hosting] (Code Cloud Learning Project LINK)

Http://git.oschina.net/tianmaxingkomg/xinjianxianmu

(run result of statistics.sh script)

Try to record "planned learning time" and "actual learning time" to see if you can improve your planning skills by the end of the semester. This work is very important and useful in learning.

Formula for time-consuming estimation

: y=x+x/n, y=x-x/n, the number of training, X, Y is close.

Reference: [Why is it so difficult to estimate a software engineering application] (http://www.cnblogs.com/xinz/archive/2011/04/05/2005978.html), [Software engineering estimation Method] (http:// www.cnblogs.com/xinz/archive/2011/04/06/2007294.html)

-Scheduled study time: 14 hours

-Actual learning time: 16 hours

-Improve the situation:

(Have time to see more [Modern software engineering courseware

Software engineer Competency Self-evaluation Table] (http://www.cnblogs.com/xinz/p/3852177.html))

# # References

-[Java Learning Note (8th Edition)] (http://book.douban.com/subject/26371167/)

-[Java Learning Note (8th Edition) Learning Guide] (http://www.cnblogs.com/rocedu/p/5182332.html)

- ...

20155335 Eucun "Java Programming" Eighth Week summary

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.