0

How can I read the names of the text files from a text file from which I want the texts are to be read?

for example, I have file.txt that has the names of several files inside it like text1.txt,txt2.txt... so I want to read all the texts from these textfiles. how can I do that in java?

Saveen
  • 4,120
  • 14
  • 38
  • 41
  • Possible duplicate of [How do I create a Java string from the contents of a file?](https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) – HamishD May 23 '18 at 16:39
  • 1
    can you maybe show what you tried so far? – Michael Lihs May 23 '18 at 16:58

4 Answers4

0

I think you can read all the text file names from file.txt and store them in a arraylist; then use each element of the arraylist as the path you gave to the FileReader.

public ArrayList<String> readWordsFromFile(){
    ArrayList<String> text = new ArrayList<>();
    try {
        FileReader fileReader = new FileReader("file.txt");
        Scanner fileIn = new Scanner(fileReader);
        text.add(fileIn.nextLine());

        fileIn.close();
        return text;

    } catch (FileNotFoundException e) {
        System.out.println("File Not Found");
        return null;
    }
}

and then :

ArrayList<String> names = readWordsFromFile();
for(String name: names){
    FileReader fileReader = new FileReader(name);
    Scanner fileIn = new Scanner(fileReader);
    //and do what ever you want with the text file
}
0

you need java.io

 import java.io.*;
    public class read {
        public static void main(String[] args) throws IOException{
            FileReader fr = new FileReader("test.txt");
            BufferedReader br = new BufferedReader(fr);

            String zeile1 = br.readLine();
            System.out.println(zeile1);
            String zeile2 = br.readLine();
            System.out.println(zeile2);
            String zeile3 = br.readLine();
            System.out.println(zeile3);

            br.close();

        }

    }
Martin Klestil
  • 574
  • 1
  • 4
  • 15
0

You could read all lines using the newer java.nio.* packages

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class ReadFiles {

  public static void main( String[] args ){
    Path filePath = Paths.get( "/path/to/file", "file.txt" );

    Charset charset = Charset.forName( "UTF-8" );

    try{
      List<String> lines = Files.readAllLines( filePath, charset );

      // all the filenames are now in the list
      // let's print them out to console.
      for( String line : lines ){
        System.out.println( line );
      }
    }catch( IOException e ){
      System.out.println( e );
    }
  }
}
blurfus
  • 13,485
  • 8
  • 55
  • 61
  • That not really new ;) it's from java 7. Upgraded with functional features in java 8. – AxelH May 24 '18 at 05:35
  • @AxelH - I didn’t say it was **new**; said it was ***newer*** (than `java.io.*;`) likely I was thinking like that because previous answers made reference to the older package – blurfus May 24 '18 at 05:51
0

To read a file, I would use Files. This is quite simple and provide some methods that allow you to read each line easily :

// Get the path for the file
Path path = Paths.get( "/tmp/files.txt" );
// Read a file a print in console.
Files.lines(path) //get a `Stream<String>`
     .forEach(System.out::println);

Now, in your case, you need to distinct action, for each line, you want to read another file. So let use a method that read a file and execute an action for each line :

public static void readFile(Path path, Consumer<String> action) {
    try {
        Files.lines(path).forEach(action);
    } catch (IOException e) {
        System.err.println(e);
    }
}

And this will be quite simple to use. For each line of the first file, we call the method to print each line in the console with (s) -> readFile(Paths.get(s), System.out::println).

Path path = Paths.get( "/tmp/files.txt" );
readFile(path, (s) -> readFile(Paths.get(s), System.out::println));

That will be a recursive call using another action to print the content.

/tmp/files.txt

/tmp/file1.txt
/tmp/file2.txt
/tmp/file3.txt

/tmp/file1.txt

Hello world

/tmp/file3.txt

Foo bar

Hello world
Foo bar
java.nio.file.NoSuchFileException: /tmp/file2.txt

Community
  • 1
  • 1
AxelH
  • 14,325
  • 2
  • 25
  • 55