Java 8 features early adopters: New New IO
In the previous article in this topic, we have seen that using Java8 lambda expressions to promote an existing JDK1.2 I/O library is primarily a way to construct an instance of Java.io.FileFilter using a lambda expression.
Many readers have pointed out that many of the APIs in java.io have been replaced by the Java.nio API in Java7, where n means new. However, Java8 brings us more surprises, and we call it new IO, although members of the Jooq community recommend it to be called Enterprise IO.
To get to the next step, we have a preliminary understanding of Java8 's new IO feature. Let's take a look at the added new method in Java.nio.Files. The most important point is that we can finally conveniently list the file under the path, Java8 inside we just need to use this new method Files.list (), this return is an inert file stream:
Files.list(new File(".").toPath()) .forEach(System.out::println);
The results of this output are:
.gitignore. \idea. \java8-goodies. Iml. \license.. \pom.. \readme.. \src. \target
It is important to note that the Foreach method is a "final method", that is, the final consumption of this stream. No one else can use this stream anymore.
If we want to filter out all the hidden files and display only the first three ordinary files, you can write this:
Files.List(Newfile (). topath ()) . Filter (p -> ! P. () . (). (. (3. Foreach (system. Out::println)
The new output will be this:
.\java8-goodies.iml.\LICENSE.txt.\pom.xml
Now it looks great. Can you give Lidian weights again? Of course. You can use the new Files.walk () method to traverse the entire directory. The following is a code example:
files. (new file (). topath ()) . Filter (p -> ! P. () . (). (. (system. Out::println)
The problem is that the code above creates a path stream that filters out all the hidden files and directories, but their sub-files are still listed. So you get the result:
过滤的:.\.git 不过却列出了:.git\COMMIT_EDITMSG.\.git\config.\.git\description[...]
Understandably, Files.walk () returns the stream of all the sub-files, and the filter method removes all hidden files, but now the walk implementation has no effect on the recursive file. This is a bit disappointing indeed. We also cannot use the Walkfiletree method in Java7 because it accepts the parameter filevisitor type is not a @functionalinterface.
However, with little improvement, you can barely solve the problem:
Files.walk (new file ( "." ). topath ()) . Filter (p -> ! P. () . (file. Separator + "." . (system. Out::println)
Now we can get the result we want:
..\Java8-Goodies.iML.\LICENSE.Txt.\Pom.Xml.\Readmetxt. \src. \src\main. \src\main\java \src\main\ java\org. \src\main\java \org\jooq[...]
The same surprise for us is the Files.lines method. The following code shows how to use its handy newline to read a file, and row by trim to filter blank lines at the same time:
Files.lines (new file ( "Pom.xml" ). topath ()) . Map (s -> s. Trim ()) . (s -> ! S. ()) . (system. Out::println)
Results of the output:
<?XmlVersion="1.0"Encoding="UTF-8"?><Projectxmlns="http://maven.apache.org/POM/4.0.0"xmlnsXSi="Http://www.w3.org/2001/XMLSchema-instance"XSischemalocation="Http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><Modelversion>4.0. 0</modelversion><groupId>org. Jooq</groupId><artifactid>java8-goodies</ Artifactid><version>1.0-SNAPSHOT</version >[...]
Summarize
The concept of lazy evaluation can make a lot of people puzzled, the same flow can only consume the situation once. I bet the problem with the IO-stream-related API for Java 8 will be the favorite of stack overflow.
Still, I have to say that the stream API is really cool. In the next installment, we'll show you how to use lambda expressions and streams to sort, and what's going to happen later is the JAVA8 for DB operations.
Original address: http://it.deepinmind.com/java/2014/02/13/Java8-features-new-new-io.html
Java 8 features early adopters: New New IO