Java 7: 包含其他文件路径的文件

Java 7: a file containing paths to other files

有什么简单的方法(在 Java 7 中)可以:

  1. 以阅读模式打开一个文件,每行包含一个到另一个文件的路径
  2. 对于每个 line/path,打开相应的文件并打印内容

(每个文件都是纯文本文件)

?

抱歉,如果这个问题很愚蠢。

谢谢

尝试这样的事情:

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();
}