Java 8 Stream – Read a file line by line
2017-12-07 14:17
281 查看
In Java 8, you can use
c://lines.txt – A simple text file for testing
Output
TestReadFile2.java
Output
TestReadFile3.java
Output
4.1
TestReadFile4.java
4.2
TestReadFile5.java
Java 8 Stream
Java BufferedReader
link: https://www.mkyong.com/java8/java-8-stream-read-a-file-line-by-line/
Files.linesto read file as
Stream.
c://lines.txt – A simple text file for testing
line1 line2 line3 line4 line5
1. Java 8 Read File + Stream
TestReadFile.javapackage com.mkyong.java8; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class TestReadFile { public static void main(String args[]) { String fileName = "c://lines.txt"; //read file into stream, try-with-resources try (Stream<String> stream = Files.lines(Paths.get(fileName))) { stream.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } }
Output
line1 line2 line3 line4 line5
2. Java 8 Read File + Stream + Extra
This example shows you how to useStreamto filter content, convert the entire content to upper case and return it as a
List.
TestReadFile2.java
package com.mkyong.java8; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util. f9f9 List; import java.util.stream.Collectors; import java.util.stream.Stream; public class TestReadFile2 { public static void main(String args[]) { String fileName = "c://lines.txt"; List<String> list = new ArrayList<>(); try (Stream<String> stream = Files.lines(Paths.get(fileName))) { //1. filter line 3 //2. convert all content to upper case //3. convert it into a List list = stream .filter(line -> !line.startsWith("line3")) .map(String::toUpperCase) .collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } list.forEach(System.out::println); } }
Output
LINE1 LINE2 LINE4 LINE5
3. BufferedReader + Stream
A new methodlines()has been added since 1.8, it lets
BufferedReaderreturns content as
Stream.
TestReadFile3.java
package com.mkyong.java8; import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class TestReadFile3{ public static void main(String args[]) { String fileName = "c://lines.txt"; List<String> list = new ArrayList<>(); try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) { //br returns as stream and convert it into a List list = br.lines().collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } list.forEach(System.out::println); } }
Output
line1 line2 line3 line4 line5
4. Classic BufferedReader And Scanner
Enough of Java 8 andStream, let revisit the classic
BufferedReader(JDK1.1) and
Scanner(JDK1.5) examples to read a file line by line, it is working still, just developers are moving toward
Stream.
4.1
BufferedReader+ try-with-resources example.
TestReadFile4.java
package com.mkyong.core; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TestReadFile4{ public static void main(String args[]) { String fileName = "c://lines.txt"; try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
4.2
Scanner+ try-with-resources example.
TestReadFile5.java
package com.mkyong.core; import java.io.File; import java.io.IOException; import java.util.Scanner; public class TestReadFile5 { public static void main(String args[]) { String fileName = "c://lines.txt"; try (Scanner scanner = new Scanner(new File(fileName))) { while (scanner.hasNext()){ System.out.println(scanner.nextLine()); } } catch (IOException e) { e.printStackTrace(); } } }
References
Java 8 File.lines()Java 8 Stream
Java BufferedReader
link: https://www.mkyong.com/java8/java-8-stream-read-a-file-line-by-line/
相关文章推荐
- Bash Shell read file line by line and substring
- bash read file line by line
- Step By Step guide to Read XML file in Java Using SAX Parser Example
- How to read file line by line?
- read file by bash shell just as getline() in c++
- Golang Read File line by line
- Read in text file line by line php - newline not being detected
- Expect script to read file line by line
- Open and read file line by line tcl tk
- Shell Script Utility To Read a File Line By Line
- How to read a file line by line?
- Read file line by line (Bash shell script, sed usage)
- Caused by: java.sql.SQLException: 无法将 NULL 值插入列 'lineid',表 'kangaroo.dbo.kangaroo_roadnet_line';该列不允许空值。INSERT 失败。
- Android遇到java.lang.RuntimeException: Binary XML file line #20这样的问题怎么办
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java流(Stream)、文件(File)和IO
- file read and write use java
- Java – Read a file from resources folder
- Read and write excel file with format by pandas and xlwt libraries of python
- 为何要在Java中使用内存映射文件(Memory Mapped File)或者MappedByteBuffer