-3

is there any simple way (in Java 7) to:

  1. open in reading mode a file containing, for every line, a path to another file
  2. for every line/path, open the respective file and print the content

(Every file is a plain text file)

?

Sorry if the question is silly.

Thank you

Sebastian
  • 15
  • 4
  • Yes. Open the file, then use a loop and a scanner to read each line. For each line, (try to) open a new file with that path, loop and print every line..then continue to the next file.Too easy :) – CubeJockey Aug 05 '15 at 16:00
  • 4
    There are many solutions to that problem. What have you tried so far, and in what way did it not work? – Tripp Kinetics Aug 05 '15 at 16:00
  • That is a straight-forward problem, you should be able do solve that at your own – Binkan Salaryman Aug 05 '15 at 16:02
  • `Files.readAllLines` might be a start to read all lines. – Joop Eggen Aug 05 '15 at 16:04
  • possible duplicate of [Read multiple lines from console and store it in array list in Java?](http://stackoverflow.com/questions/11621302/read-multiple-lines-from-console-and-store-it-in-array-list-in-java) – Nathaniel Ford Aug 05 '15 at 16:37

1 Answers1

0

try something like this:

public static void main(String[] args) throws IOException {
    // open stream to path list file
    InputStream indexSource = new FileInputStream("index.txt");

    // create reader to read content
    try(BufferedReader stream = new BufferedReader(new InputStreamReader(indexSource))) {
        // loop
        while (true) {
            // read line
            String line = stream.readLine();
            if (line == null) {
                // stream reached end, escape the loop
                break;
            }
            // use `line`
            printFile(line);
        }
    }
}

static void printFile(String path) throws IOException {
    // open stream to text file
    InputStream textSource = new FileInputStream(path);

    // print file path
    System.out.println("### " + path + " ###");
    // create reader to read content
    try(BufferedReader stream = new BufferedReader(new InputStreamReader(textSource))) {
        // loop
        while (true) {
            // read line
            String line = stream.readLine();
            if (line == null) {
                // stream reached end, escape the loop
                break;
            }
            // print current line
            System.out.println(line);
        }
    }
    // nicer formatting
    System.out.println();
}
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29