zoukankan      html  css  js  c++  java
  • Java 8 Stream – Read a file line by line

    In Java 8, you can use Files.lines to 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.java

    package 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 use Stream to 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.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 method lines() has been added since 1.8, it lets BufferedReader returns 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 and Stream, 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();

    }

    }

    }

    from:http://www.mkyong.com/java8/java-8-stream-read-a-file-line-by-line/

    In Java 8, you can use Files.lines to 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.java
    package 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
  • 相关阅读:
    sitemap怎么制作才适合蜘蛛抓取?
    网站高并发优化性能调优总结
    圆柱模板行业B2B站点打造MIP推送+熊掌号推送+历史普通推送插件
    a href="javascript:void(0)" 是什么意思?加不加上有什么区别?
    <a href="#" onclick="history.back();"></a>这样写为什么是对的? -(转)
    form 表单 enctype 属性-(转自w3c)
    关于submit与document.form1.submit();这2个提交的区别
    zf-表单填写以及相关业务流程
    zf-关于把某个地址的svn项目移动到另一个上面的步骤
    zf-关于即将过期提示字符串的修改
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/10073605.html
Copyright © 2011-2022 走看看