Search code examples
javajava.util.scanner

java read files from a directory line by line


I have a directory with text files, those files contain a number in every line, which I have to do some calculations with. I need to read a file, read every single line in the file and then continue with another file until they all are read. I am new to Java so I was thinking of using scanner to read every line, but I haven't been able to get a file from a directory correctly.

private String Folder_path = "the path to dir";
Path kazkas = Paths.get(Folder_path);
File[] files = new File(Folder_path).listFiles();

I get to this point, but haven't found a solution how to take single file and pass it to the scanner.


Solution

  • My solution:

    private String Folder_path = "the_path_to_fikes";
    File[] files = new File(Folder_path).listFiles();
    String obj = files[1].toString();
        Path path = Paths.get(obj);
        try {
            Files.lines(path).forEach(line -> {
                System.out.println(line);
            });
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }