-2

I need to read through several text files, all located within the same directory. I need to loop through each line of the text file, parse the line out, perform some processing, then read the next line. When I get to the last line, I want to read the next file in the directory, if another file exists. Does anyone know of a way to read through each file within a directory? ie. All lines in file1, then all lines in file2, then al lines in file3. I know how to read through the text lines, but I do not know how to read the next file in the directory. The files names may not be known in advance, but I still need to read the next file available.

Example

File1.txt
  - Data line 1
  - Data line 2
  - Data line 3
File2.txt
  - Data line 1
  - Data line 2
File7.txt
  - Data line 1
  - Data line 2
  - Data line 3
Grice
  • 1,345
  • 11
  • 24
Aman Khan
  • 1
  • 1
  • 1
  • [Converting file to string in Java](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file/326440#326440) – Grice Sep 24 '14 at 19:26
  • Welcome to SO. Your question is way too broad. It might be better to first try something like the [Basic I/O Tutorial](http://docs.oracle.com/javase/tutorial/essential/io/). – vanza Sep 24 '14 at 19:33

2 Answers2

1

I will give you a place to start. Look at the Java File and Scanner class. You can use the file class to create an object based on a directory then call listFiles() on the File object to get the files. You can then loop through these files and use the Scanner class to read each file line by line Scanner.nextLine(). This should give you a place to start. Try figuring some of it out yourself and if you still have problems post the code you tried and we can take a look at it and help you.

brso05
  • 13,142
  • 2
  • 21
  • 40
1

You can use Files.walkFileTree . It will allow you to pass the directory from which you want to read and you can write your own Visitor to process files in whatever way you want. Demo

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class MultipleDemo extends SimpleFileVisitor<Path> {

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        System.out.printf("----------\tReading %s\t-------", file.getFileName());
        BufferedReader bufferedReader = Files.newBufferedReader(file);
        String line = bufferedReader.readLine();
        while (line != null) {
            System.out.println(line);
            line = bufferedReader.readLine();
        }

        System.out.println("Finished");
        return FileVisitResult.CONTINUE;
    }

    public static void main(String[] args) throws IOException {
        Files.walkFileTree(Paths.get("full/path/to/your/directory"), new MultipleDemo());
    }
}
sol4me
  • 15,233
  • 5
  • 34
  • 34