201711671224 "Java Programming" Chapter 14 learning summary

Learning content summary

First, know NIO

1.NIO narrative

  Chapter X introduced based InputStream. OutputStream, reader, Writer, input and output. For advanced input / output processing, Java JDK1.4 from the beginning to provide a NIO (New IO), and Java SE7 in turn provides NIO2, recognize and take advantage of these advanced input / output processing API, the input processing efficiency / output will a great help. InputStream, OutputStream input / output, basically bytes for low-level processing, although you have to directly face an array of bytes, but in fact mostly byte array entire block for processing. For example seen in Section 10.1.1 dump () method, in fact, after reading the data block, and the entire write data fast, but you have to deal with the number of bytes byte [], the read must be recorded, You must specify write byte [] of origin and the number of bytes.

  Although java.io package also has some decorative, but if as long as bytes or blocks of interest in string handling, these classes will not necessarily appropriate, must write its own API, or find related link library to handle the index , marks and other details. With respect to the stream input / output use InputStream, OutputStream adapter to the source and destination data, to channel adapter using the NIO data nodes, when processing the data, the NIO allows you to set the buffer capacity in the buffer of interest marked data block for which the block flag, a clear (), rewind (), flip (), compact () advanced operations.

2.Channel architecture and operation

   NIO in Channel interfaces associated with the class, is located in java.nio.channels suite, Channel interface is a AutoClosable sub-interfaces, and therefore are trying to shut down after JDK7 can use grammar resources, added isOpen on Channel Interface () method, Channel used to confirm whether or not open. For starters, you can first get to know the main Channel inherited architecture as shown.

 

   ByteChannel does not define any method, simply inherits the behavior ReadableByteChannel and WritableByteChannel, ByteChannel SeekableByteChannel sub-interfaces can be read with a change to the next data access position.

 

1.3Buffer architecture and operation

  NIO in the design, the data is processed in the java.nio.Buffer, Buffer is an abstract class that defines the clear (), rewind (), flip (), compact () advanced operations on the data block, such operating return types are Buffer, in fact, return to this.

    Buffer class inherits architecture

 

 

Two, NIO2 file system

1.NIO2 architecture

NIO2 file system API provides a set of standard interfaces and classes, as long as the application developer based on these standard interfaces and classes like file system operation. Application developers are mainly used java.nio.file and java.nio.file.attribute, abstract class or interface packet must be operated by a provider operating a file system. Center NIO2 file system is java.nio.file.spi.FileSystemProvider, itself an abstract class. File system provider is a class operation. Effect is generated and the operation target java.nio.file java.nio.file.attribute various abstract class or interface.

2. The operating path

Want to get Path instance, you can use Paths.get () method, using the most basic way is to use the string path, you can use a relative path and absolute path. Examples merely typify Path route information, the route corresponding to the actual document or folder not necessarily present.

 

Documentation and directory operations

如果想要删除Path代表的文档或目录,可以使用Files.delete()方法,如果不存在,会抛出NoSuchFileException,如果因目录不为空而无法删除文档,会抛出DirectoryNotEmptyException。使用Files.deleteIfExists()方法也可以删除文档,这个方法在文档不存在时调用,并不会抛出异常。Files.copy()还有两个重载版本,一个是接受InputStream作为来源,可直接读取数据,并将结果复制至指定的Path中;另一个Files.copy()版本是将来源Path复制至指定的OutputStream。若要进行文档或目录移动,可以使用Files.move()方法,使用方式与Files.copy()方法类似,可指定来源Path、目的地Path与CopyOption。

.过滤、搜索文档

    如果想在列出目录内容时过滤想显示的文档,例如只想显示.class与.jar文档,可以在使用Files.newDirectoryStream()时,将第二个参数指定过滤条件为*.{class,jar}。Files.newDirectoryStream()的另一版本接受DirectoryStream.Filter接口操作对象,如果Glob语法无法满足条件过滤需求,可以自行操作DirectoryStream.Filter的accept()方法自定义过滤条件。

代码调试中的问题和解决过程

  • 暂无

其他(感悟、思考等)

参考资料

  • 《Java程序设计》

Guess you like

Origin blog.csdn.net/nemeziz/article/details/85042782