14th Chapter
I. Knowledge of NIO
1.NIO Narration
For advanced input/output processing, Java provides the NIO (New IO) from JDK1.4, while Java SE7 provides NIO2 to recognize and utilize these advanced input/output processing APIs, which can be very useful for the processing efficiency of input/output. InputStream, OutputStream input/output, basically in bytes for low-level processing, although you have to face the byte array directly, but in fact, most of the byte array in the entire block processing. Although there are some decorations in the Java.io suite, these classes are not suitable if they are processed in bytes or chunks of interest in the string, they must compose their own APIs or find relevant link libraries to handle the details of indexes, tags, and so on. In contrast to stream input/output using InputStream, outputstream to connect data sources and destinations, NIO uses channels to connect data nodes, and when processing data, NIO allows you to set the buffer capacity, marking the chunks of interest in the buffer, For these block tags, advanced operations such as clear (), rewind (), flip (), compact () are provided.
2.Channel Architecture and operations
The channel-related interfaces and classes in NiO, is located in the Java.nio.channels suite, the channel interface is the sub-interface of the autoclosable, so you can use the JDK7 after the attempt to close the resource syntax, the channel interface has a new IsOpen () method to confirm that the channel is open. Bytechannel does not define any method, simply inherits the behavior of Readablebytechannel and Writablebytechannel, Bytechannel sub-interface Seekablebytechannel can read and change the next location to access data.
3.Buffer Architecture and operations
In the NIO design, Data is processed in Java.nio.Buffer, buffer is an abstract class that defines the advanced operations on data chunks such as clear (), rewind (), flip (), compact (), which are all buffer and actually return this. The size of the buffer can be obtained with the capacity () method, and the data bounds index value of the buffer can be obtained with the limit () method, and the position index value of the next readable data of buffer can be obtained by the position () method. When a buffer has just been configured or called the Clear () method, limit () equals capacity (), and position () is 0.
When the Rewind () method is called, the position is set to 0, and the limit is constant, which is usually used when you want to read a piece of data in buffer repeatedly, acting as the position (0) method that calls buffer alone.
Ii.. NIO2 File System
1.nio2 Architecture
The NIO2 file System API provides a standard set of interfaces and classes that application developers can do with a file system based on these standard interfaces. Application developers primarily use Java.nio.file and Java.nio.file.attribute, an abstract class or interface that must be manipulated in a package that is operated by a file system provider. The center of the NIO2 file system is Java.nio.file.spi.FileSystemProvider, which itself is an abstract class. is the class that the file system provider operates. The function is to produce java.nio.file and Java.nio.file.attribute objects of various abstract classes or interfaces.
2. Operation Path
To get the path instance, you can use the Paths.get () method, the most basic way, is to use the string path, you can use the relative path and absolute path. The path instance represents only the path information, and the actual document or folder that corresponds to it does not necessarily exist.
Operation Result:
3. property reading and setting
In the past, there was no standard way to obtain different attributes supported by different file systems, and in JDK7, you could obtain supported attribute information for different file systems.
Operation Result:
Operation Result:
4. Operating Documents and directories
If you want to delete the document or directory that the path represents, You can use the Files.delete () method, which throws a nosuchfileexception if it does not exist, and throws a directorynotemptyexception if the document cannot be deleted because the directory is not empty. You can also delete a document using the Files.deleteifexists () method, which is called when the document does not exist and does not throw an exception. Files.copy () also has two overloaded versions, one that accepts InputStream as the source, can read the data directly, and copies the results to the specified path, and the other Files.copy () version is the copy of the source path to the specified outputstream. To move a document or directory, you can use the Files.move () method, similar to the Files.copy () method, to specify the source path, destination path, and copyoption.
5. Read and access the directory
If you want to obtain the file system root path information, you can use the FileSystem getrootdirectory () method.
Operation Result:
6. Filter, Search for documents
If you want to filter the documents that you want to display when you list the contents of the catalog, for example, you want to display only the. class and. Jar documents, you can specify a filter condition of * For the second parameter when using Files.newdirectorystream (). {Class,jar}. Another version of Files.newdirectorystream () accepts the Directorystream.filter interface operand, and if the GLOB syntax does not meet the conditional filtering requirements, you can manipulate the Directorystream.filter's accept () method from Define the filter criteria.
Operation Result:
15th Chapter
First, the log
1. Introduction to the Log API
The Java.util.logging package provides logging functionality related classes and interfaces. The benefit of using the standard Java platform is that you do not have to configure the log component in addition, the starting point for using the log is the logger class, and the constructor for the logger class is labeled protected. Classes that are not java.util.logging are not created directly with new, and to get logger instances, you must use Logger's static method GetLogger (). When you call GetLogger (), you must specify the namespace to which the logger instance belongs. Namespace with "." Differentiated as a hierarchy. Logger with the same namespace level, with the same parent logger configuration. The namespace is named as the class full name, usually in which class the logger is obtained. Simply put, logger is the starting point of recording information, to output the information, must first through the level and filter logger, and then through the level of handler filter, format information to formatter, The action of outputting information is actually handler responsible.
Operation Result:
2. Specify the log level
LOGGER and handler by default will first filter the information according to level, if not made any changes, the LOGGER instance of the parent LOGGER configuration, is Logger.globalLOGGER The name namespace logger the configuration of the instance, the configuration of this instance is set to info, the parent logger instance can be obtained through the getparent () of the logger instance, and a set level instance can be obtained by Getlevel (). The information processing of LOGGER will propagate to the parent LOGGER, that is to say, without any configuration setting, the LOGGER instance obtained by default must be greater than or equal to Logger.globalLOGGER The name namespace logger The level.info of the instance setting, it is possible to output information.
Operation Result:
3. Using handler and Formatter
Memoryhandler does not format the log information, the information is staged in the memory buffer until the buffer size is exceeded to output the information to the specified destination handler. Streamhandler can specify the OutputStream instance that is used when the information is output by itself, and the subclass uses the specified formatter formatting information, Consolehandler when it is created, OutputStream is automatically specified as System.err, so the log information is displayed on the console. Filehandler the FileOutputStream used when creating the log output when it is created, the document location and name can be specified using the pattern string.
4. Customizing handler, Formatter, Filter
If the handler results provided in the Java.util.logging package do not meet the requirements, you can inherit the handler class, manipulate the abstract method publish (), flush (), close () from the definition handler, and recommend that you consider the filtering and formatting of the information.
Operation Result:
5. Using Logging.properties
It is very convenient to change the configuration of the Logger object by using the method of writing the program, in fact, the logger configuration can be set by Logging.properties.
Ii. the basis of internationalization
1. Using ResourceBundle
The static Getbundle () method of ResourceBundle takes a ResourceBundle instance, the given argument name is the main file name of the information document, and Getbundle () automatically finds the corresponding. Properties document.
2. Use locale
Three important concepts of internationalization: region information, resource bundle and base name.
Third, the rule expression
1. Introduction to Rule expressions
The rule expression is mainly used in character, string format comparison, and later in the field of information widely used. The rule representation basically consists of two characters: literal meaning word and metacharacters character.
Operation Result:
The following example uses String ReplaceAll () to demonstrate the difference between the 3 quantifiers.
Operation Result:
The following are examples of boundary comparisons:
Operation Result:
Operation Result:
2.Pattern and Matcher
The Java.util.regex.Pattern instance is the representative object of the rule representation in the JVM, and the pattern's constructor is marked as private, so you cannot create the pattern instance with new and must be obtained through the static method compile () of the pattern.
Operation Result:
The following example allows you to enter a rule expression with the string you want to compare:
Operation Result:
Iv. JDK8 API Enhancements
1.StringJoiner, arrays new API
If you have a set of strings that you want to specify as a comma-delimited link between each string, the string adds a join () static method that can be used directly, not only the list, this version of join () can actually accept the iterable operand. The Paralleprefix (), Parallesetall (), and Parallesort () methods are added to the arrays.
2.Stream related APIs
If you want to pipe an array, one method is to use arrays's aslist () method to return to the list, and then call the stream () method to get the stream instance. Another way is to use the arrays stream () method, which can specify an array after the stream instance is returned.
Problems in teaching materials learning and the solving process