| 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 792: |
Line 792: |
| | 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 === | + | ===List files in dir=== |
| | <syntaxhighlight lang="java"> | | <syntaxhighlight lang="java"> |
| | import java.io.IOException; | | import java.io.IOException; |
| Line 887: |
Line 887: |
| | }</source> | | }</source> |
| | | | |
| | + | === with nio === |
| | + | <syntaxhighlight lang="java"> |
| | + | import java.io.IOException; |
| | + | import java.nio.file.Files; |
| | + | import java.nio.file.Path; |
| | + | import java.nio.file.Paths; |
| | + | import java.util.List; |
| | + | |
| | + | public class FileReadRunner { |
| | + | public static void main(String[] args) throws IOException { |
| | + | Path pathFileToRead = Paths.get("./resources/data.txt"); |
| | + | List<String> lines= Files.readAllLines(pathFileToRead); |
| | + | System.out.println(lines); |
| | + | } |
| | + | } |
| | + | |
| | + | public class FileReadLineByLineRunner { |
| | + | public static void main(String[] args) throws IOException { |
| | + | Path pathFileToRead = Paths.get("./resources/data.txt"); |
| | + | Files.lines(pathFileToRead).forEach(System.out::println); |
| | + | Files.lines(pathFileToRead).map(String::toLowerCase).forEach(System.out::println); |
| | + | Files.lines(pathFileToRead).map(String::toLowerCase).filter(str -> str.contains("a")).forEach(System.out::println); |
| | + | } |
| | + | } |
| | + | </syntaxhighlight><br /> |
| | ===Writing files=== | | ===Writing files=== |
| | <source lang="java"> | | <source lang="java"> |