Read a File in Java Stream Full Examples

By | April 16, 2018

java-programming-language-logo-imagesRead a File in Java – I’m so happy because in Java 8 you can use Files.lines to read file as Stream. In this tutorial, i’ll show to you how to read a file using Java Stream with Java 10 (Read a File in Java).

1. Java Read File + Stream

package com.cimblo.javacodeexample.read;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author Cimblo.com
 * @since 16/04/2018
 */
public class ReadFileJavaExample1 {

    /*you can use Files.lines to read file as Stream.*/

    public static void main( String[] args ){
        var fileName = "c://javaTutorialAndExamples.txt";

        //read file into stream, try-with-resources
        try (var stream = Files.lines(Paths.get(fileName))) {

            stream.forEach(System.out::println);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

2. Java 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.

package com.cimblo.javacodeexample.read;

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;

/**
 * @author Cimblo.com
 * @since 16/04/2018
 */
public class ReadFileJavaExample2 {

    public static void main( String[] args ){
        var fileName = "c://javaTutorialAndExamples.txt";
        List list = new ArrayList<>();

        try (var 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);
    }
}

3. BufferedReader + Stream

A new method lines() has been added since 1.8, it lets BufferedReader returns content as Stream.

package com.cimblo.javacodeexample.read;

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;

/**
 * @author Cimblo.com
 * @since 16/04/2018
 */
public class ReadFileJavaExample3 {

    public static void main( String[] args ){
        var fileName = "c://javaTutorialAndExamples.txt";
        List list = new ArrayList<>();

        try (var 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);
    }

}

4. Classic BufferedReader And Scanner

Enough of Java 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.

package com.cimblo.javacodeexample.read;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Cimblo.com
 * @since 16/04/2018
 */
public class ReadFileJavaExample4 {

    public static void main( String[] args ){
        var fileName = "c://javaTutorialAndExamples.txt";

        try (var 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.

package com.cimblo.javacodeexample.read;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author Cimblo.com
 * @since 16/04/2018
 */
public class ReadFileJavaExample5 {

    public static void main( String[] args ){
        var fileName = "c://javaTutorialAndExamples.txt";

        try (var scanner = new Scanner(new File(fileName))) {

            while (scanner.hasNext()){
                System.out.println(scanner.nextLine());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

That’s how to “Read a File in Java”. Next you can find more tutorial about read file with java code in this page.

References

  1. Java 8 File.lines()
  2. Java 8 Stream
  3. Java BufferedReader

Leave a Reply