| Line 3: |
Line 3: |
| | ===With apt=== | | ===With apt=== |
| | <nowiki>sudo apt-add-repository ppa:webupd8team/java | | <nowiki>sudo apt-add-repository ppa:webupd8team/java |
| − | sudo apt-get update
| + | sudo apt-get update |
| − | sudo apt-get install oracle-java8-installer</nowiki>
| + | sudo apt-get install oracle-java8-installer</nowiki> |
| | | | |
| | Also ensure your JAVA_HOME variable has been set to: | | Also ensure your JAVA_HOME variable has been set to: |
| Line 791: |
Line 791: |
| | ===Reading text files=== | | ===Reading text files=== |
| | See: [https://github.com/rafahsolis/javaTutorial/blob/master/ReadTextFile.java ReadTextFile.java] | | See: [https://github.com/rafahsolis/javaTutorial/blob/master/ReadTextFile.java ReadTextFile.java] |
| | + | |
| | + | === List files in dir === |
| | + | <syntaxhighlight lang="java"> |
| | + | import java.io.IOException; |
| | + | import java.nio.file.Files; |
| | + | import java.nio.file.Paths; |
| | + | import java.util.function.Predicate; |
| | + | |
| | + | Path currentDirectory = Paths.get("."); |
| | + | Files.list(currentDirectory).forEach(System.out::println); |
| | + | |
| | + | int levels = 5; |
| | + | Files.walk(currentDirectory, levels).forEach(System.out::println); |
| | + | |
| | + | // list only .java files |
| | + | Predicate<? super Path> predicate = path -> String.valueOf(path).contains(".java"); |
| | + | Files.walk(currentDirectory, levels).filter(predicate).forEach(System.out::println); |
| | + | |
| | + | // Search files |
| | + | BiPredicate<Path, BasicFileAttributes> javaMatcher = (path, attributes) -> String.valueOf(path).contains(".java"); |
| | + | Files.find(currentDirectory, levels, javaMatcher).forEach(System.out::println); |
| | + | |
| | + | BiPredicate<Path, BasicFileAttributes> dirMatcher = (path, attributes) -> attributes.isDirectory(); |
| | + | Files.find(currentDirectory, levels, dirMatcher).forEach(System.out::println); |
| | + | |
| | + | </syntaxhighlight> |
| | + | |
| | ===Reading files (Try-With-Resources)=== | | ===Reading files (Try-With-Resources)=== |
| | This requires at least Java 7<br /> | | This requires at least Java 7<br /> |
| Line 990: |
Line 1,017: |
| | </syntaxhighlight> | | </syntaxhighlight> |
| | | | |
| − | === catch multiple exceptions === | + | ===catch multiple exceptions=== |
| | <syntaxhighlight lang="java"> | | <syntaxhighlight lang="java"> |
| | try { | | try { |