Read a file line by line

    4

    102

    javafile
    Java Public Recipes

    Read a file line by line

    Java function to read a file line by line.

    import java.io.*;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.*;
    import java.util.*;
    
    public static List<String> readAllLinesInFile(String fileName)
    {
      List<String> lines = Collections.emptyList();
      try
      {
        lines =
          Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
      return lines;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.