15th General API15.1 Log 15.1.1 Log API Introduction
1, java.util.logging package provides the log function related classes and interfaces, do not need to configure the log components, can be used in the standard Java platform is its advantage. The starting point for using the log is the logger class, and the constructor for the logger class is labeled protected. A class that is not a java.util.logging package cannot be created directly with new, and to obtain an logger instance, the Logger static method GetLogger () is required.
2. When calling GetLogger (), you must specify the namespace to which the logger instance belongs, with the namespace "." As a hierarchical distinction, the logger of the same namespace level, whose parent logger is configured identically.
The namespace is named as the class full name, usually in which class the logger is obtained.
After you have obtained the logger instance, you can use the static members of the lever to specify the information level.
3, Logger is the starting point to record information, to output the information, must first through the logger lever and filter filtering, in the handler through the lever and filter filtering, formatting information to the action of formatter, The action of outputting information is actually handler responsible.
4, Logger has a hierarchical relationship, the same namespace level logger, the parent logger configuration will be the same, each logger after processing their own log action, will propagate to the parent logger, so that the parent logger can also process the log.
15.1.2 specifying the log level
1, LOGGER instance of the parent LOGGER configuration, is Logger.globalLOGGERname namespace LOGGER instance, can be getlever () to obtain a set of lever instances.
2, LOGGER information processing will be transmitted to the parent LOGGER, in the absence of any configuration settings, the default acquisition of LOGGER instances, the hierarchy must be greater than or equal to Logger.globalLOGGER The name namespace logger The lever.info of the instance setting, it is possible to output information.
3, through Setlever () set the lever instance, if log () The specified lever instance contains an int value less than logger set of lever instance contains the int value, lever will not log information.
4, after logger filtering, but also through the handler filter, a logger can have more than handler, can be Logger addhandler () add handler instance. In fact, when the information output, the current logger handler processing, will also be propagated to the parent logger all handler processing, the Gethandlers () method can be obtained by the current handler instance array.
5, in the absence of any configuration settings, obtained LOGGER instance, will only use Logger.globalLOGGERname noun space LOGGER instance has handler, the default is to use Consolehandler, As a subclass of handler, the function is to output log information under the console, and the default level is Lever.info.
15.1.3 using handler and formatter
1, responsible for the log output is handler instance.
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, and it will use the specified formatter formatting information with subclasses.
When Consolehandler is created, OutputStream is automatically specified as System.err, so the log information is displayed in the console.
Filehandler the FileOutputStream that is used when creating the log output when it is created, the document location and name can be specified using pattern string strings.
Sockethandler can specify the host location and port when it is created, the internal network is automatically established, and the log information is transferred to the specified host.
2. Logger can use AddHandler () to add handler instances and remove handler using RemoveHandler ().
3. Specify the pattern string in Create Filehandler:
Use "%h" to represent the user's root directory
Using "%t" to obtain the system staging directory
Use "%g" to automatically number documents
Using "%h/config%g.log" means that the ConfigN.log file is stored in the user root directory, and n indicates the document number of each information that is automatically incremented.
4, Filehandler The default formatter is Xmlformatter;
Consolehandler uses Simpleformatter by default, these two classes are subclasses of formatter and can be set setformatter by the formatter () method of handler.
5. If you want to output information or store a document with a specific encoding, Handler has a setencoding () method to specify the text encoding.
15.1.4 customizing Handler, formatter, and filter
1, if the handler results provided in the Java.util.logging package do not meet the requirements, you can inherit the handler class, the Operation abstract method Publish (), the Flush (), the Close () method to customize handler.
2, in the allocation of responsibilities, handler is responsible for the output, format is handed to formatter, and information filtering is to filter. Handler has a default isloggable () operation that filters information based on level and then filters the information using the specified filter.
The format () method passes in the LogRecord and stores all the log information.
3, in addition to the level of filtering, you can also add additional filtering conditions, you can operate the filter interface.
15.1.5 using Logging.properties
Ways to change the configuration of a logger object:
(1) How to use the program writing
(2) Set the logger configuration by logging.properties.
15.2 Internationalization Foundation 15.2.1 use ResourceBundle
1. Localization and internationalization
Localization: Applications are based on users in different regions, rendering different languages, and date formats.
Internationalization: When you design your application, you can use different languages, date formats, and so on for different users without modifying your application.
2, for the future may change the text information, you can consider moving the information outside the program, by using ResourceBundle to do information binding. The static Getbundle () method of ResourceBundle takes a resourcebundle instance, given the argument name is the main file name of the information document.
15.2.2 using locale
1. The three important concepts of internationalization are regional information , Resource Package , and base name .
Area information: Represents a specific geographic, political, or cultural area that can be specified by a language encoding and an optional locale code. Where the language encoding is ISO-639 defined, represented by two lowercase letters. The area code is represented by two uppercase letters, defined in ISO-3166.
Region information: The corresponding class is locale.
Resource bundle: Includes information about a specific region, the ResourceBundle object, which is the representative object of the resource bundle in the JVM. Each resource bundle that represents a different region of the same set of information will share the same underlying name.
Base name: The name specified when using ResourceBundle's Getbundle (), which is the base name specified.
2. To create a message.zh_TW.properties that must be represented by Unicode encoding, you can use the JDK utility native2ascii to assist the conversion.
If you want the. Properties of the Unicode encoding to be reversed in Chinese, you can use the-reserve argument.
3, when using ResourceBundle, how to obtain the corresponding information document according to the base name:
(1) Obtaining information documents using the specified Locale object
(2) Use Locale.getdefault () to obtain information documents
(3) Use the base name to obtain the information document.
15.3 Introduction to the rule expression 15.3.1 rule expression
The rule expression basically consists of two characters: literal meaning character and meta-character .
literal meaning characters are characters that are compared by literal means;
Metacharacters characters that have different meanings in different contexts when not in the literal comparison.
1. Literal meaning character
Letters and numbers in regular expressions are compared in literal terms, and some characters are treated as metacharacters after they have been added \.
Metacharacters have special meanings in regular expressions, such as! $ ^ ( ) + = { } [ ] | \ : 。 ? such as If you are unsure of which punctuation characters to add to the ignore characters, you can add \ to each punctuation mark.
You can use the split () method to cut.
Example: The rule expression \|\| compose as a Java string is "\|\|".
2. Character class
In a rule expression, multiple characters can be grouped together to become a character class. The character class compares the text to see if any of the characters conform to a character in the character class.
One way to categorize characters is to place characters in [].
[] The characters in the "or" mean, | In a character class is just a literal character, and will not be represented as "or". Hyphens-as character-class metacharacters, representing a range of text. The character class can use ^ as the character class metacharacters, and [^] as the inverse character class.
Predefined character classes
. : any one character
\d: Compare any numeric character
\d: Compare any non-numeric characters
\s: Compare any whitespace
\s: Compare any non-whitespace
\w: Compare any ASCII character
\w: Compare any non-ASCII character
3, greed, gradual, single spit measure
Greedy quantifiers: Find the longest-fit text as much as possible
Gradual quantifiers (after greedy quantifiers added?) ): Find the shortest possible matching text (lazy quantifier, non-greedy quantifier)
Single Spit quantifier (after greedy quantifiers plus +): Comparator saw the word is, will first eat the remaining text, and then see if the words of the part of the word is consistent with eating.
4. Boundary comparison
The boundary comparison is used to indicate that the text must conform to the specified boundary condition, which is the anchor point, so this type of expression is also often referred to as anchors.
5. Grouping and reference
You can use () to group regular expressions, which can be used with quantifiers in addition to being a sub-rule representation.
When grouping back reference, it is to add a grouping count after \, which indicates the comparison result of the reference number group.
15.3.2 pattern and Matcher
1. The Java.util.regex.Pattern instance is the representative object of the regular expression in the JVM, and the pattern constructor is marked as private, and the method for creating the pattern instance is:
(1) through the static method of pattern compile () to obtain
(2) Designation of flags (flag)-embedded flag notation.
2, after obtaining the pattern instance, the method that can be used is:
Split () Method: Cut the specified string by regular expression
Matcher () Method: Specify the string to compare
Find () method: see if there is a next matching string
Lookingat (): see if the beginning of the string matches the rule expression
Group (): You can return a string that matches.
3, after obtaining the Matcher instance, the methods that can be used are:
ReplaceAll () can replace the part of a rule expression with a specified string
Replacefirst () replaces part of the first combined rule expression
Replaceend () replaces the last part that conforms to the rule expression
The start () method can obtain an initial index that conforms to the string
The end () method can get an index that matches the last character of the string
15.4 JDK8 API Enhancements 15.4.1 Stringjoiner, arrays add API1, String.Join (), Stringjoiner
In JDK8, String adds a join () static method that can be used directly, join () accepts the charsequence operand, and string is one of them.
2, Arrays
In JDK8, arrays added the Parallelprefix (), Parallelsetall (), and Parallelsort () methods.
Parallelprefix () method hospital Specifies the Xxxbinaryoperator instance, which executes a stream-like reduce () method.
The Parallelsetall () method is used to initialize an array or to completely reset each index element.
The Parallelsort () method allows you to divide the specified array into sub-arrays and sort them separately at the time of parallelism.
15.4.2 Stream related APIs
1, the method of pipeline operation for the array:
(1) Use arrays's Aslist () method to return to list, and then call the stream () method to get the stream instance (2) using the arrays stream () method, you can specify the array to return the stream instance.
2, charsequence on the new chars () and codepoints () The law, are returned Intstream, the former represents a string of characters of the integer value, the latter represents a string of words Fu code point.
3, Math in JDK8 also added a lot of math run related APIs, such as Multiplluexact (), Floormod (), Floordiv () and so on.
Java JDK8 Learning Notes--15th general API