1

I'm new here so let me know if I'm going against any rules with this question.

I have a Java app that's doing what's needed: it gets N .txt files, creates 1 thread per file, and then creates an inverted index out of all these files, outputting the result into a single .txt file.

The output is how it's supposed to be, the thread management is working just fine, but there's still something I want to improve. This is how I'm grabbing the source files:

static String Docs [] = {"path/to/file1.txt", "path/to/file2.txt"}

But I wanted to do it automatically instead of having to list all files one by one. Imagine I have like 20,000 files, it would take a lot of time to do so.

I didn't find anything related to it and my professor says it's alright the way it is, but I wanted to know how to simply feed this variable with the files inside X path instead of having to list all files.

  • 2
    Take a look at https://stackoverflow.com/questions/7301764/how-to-get-contents-of-a-folder-and-put-into-an-arraylist. –  Apr 29 '18 at 19:23
  • Possible duplicate of [How to get contents of a folder and put into an ArrayList](https://stackoverflow.com/questions/7301764/how-to-get-contents-of-a-folder-and-put-into-an-arraylist) – Ruslan Akhundov Apr 29 '18 at 21:35

1 Answers1

0

You can use this code to scan a local folder and populate the file paths in the Docs array.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class GetFilesFromDirectory {

    public static void main(String[] args) {
        String folderPath = "/Users/js/test";
        int depth = 1;

        List<String> DocsList = getFiles(folderPath, depth);
        String[] Docs = new String[DocsList.size()];

        for (int i = 0; i < DocsList.size(); i++) {
            Docs[i] = DocsList.get(i);
        }

        //for check
        for (String Doc : Docs) {
            System.out.println(Doc);
        }
    }

    private static List<String> getFiles(String pathToSearch, int depth) {
        List<String> files = new ArrayList<>();
        try {
            Stream<Path> paths = Files.find(Paths.get(pathToSearch), depth, (path, attributes) -> attributes.isRegularFile());
            paths.forEach(path -> files.add(path.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return files;
    }
}

Test run:

Say the local disk has a folder with some files in it.

$ ls -l /Users/js/test
file1.txt
file2.txt
file3.txt

Running the code with folderPath = /Users/js/test, and depth = 1 will give:

/Users/js/test/file1.txt
/Users/js/test/file2.txt
/Users/js/test/file3.txt
Jagrut Sharma
  • 4,574
  • 3
  • 14
  • 19