Reading a lot of files without hardcoding their path

This may be more of a "best practice" question, but I am trying to use two text documents in my current project and am not sure how best to continue reading and / or storing. I currently have the paths to them hardcoded in my filleader, which is definitely not what I want (below for the current implementation).

try (BufferedReader reader = new BufferedReader(new FileReader("C:\\e\\t\\c\\.\\text.txt"))) {
        String line;
        while ((line = reader.readLine()) != null) {
            string1 = string1 + " " + line;
        }
    }

      

This works, but I will need to change the file location for each computer it uses.

What I have tried:

  • Hard-coding the file path (works, not flexible at all)
  • Creating a local file path through my project (I ran into some difficulties and couldn't figure it out)
  • Copy / paste several thousand plain text files directly into lines in my program (seems really ugly, but I'm not sure if something should / shouldn't be done)

I'm still pretty new to Java and programming in general, so please let me know if you need more information or if there is anything else I have to supply (or if there is a similar question I missed in my search). Thanks everyone!

EDIT:

About the functionality of my program: What I am doing is take a string that the user enters and then check it against the text documents I am trying to load to see how many matches (similar to using "ctrl + f", for each a single word you're trying to find on a page / document, where the page / document is a library of words I'm trying to load).

I had to take a short break, but now I will try your suggestions. It all looks great and grateful!

+3


source to share


3 answers


( , ), , . , "src/", , "src/" "res/" . , , . , com.example. *, text.txt res/com/example/ , , :

try (InputStream in = getClass().getResourceAsStream("/res/com/example/text.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
        String line;
        while ((line = reader.readLine()) != null) {
            string1 = string1 + " " + line;
        }
}

      



However, if text files are to be provided by the user's system, then they are external resources, in which case the path to them must be provided at runtime. Typically, as arguments to the program itself. So, in your method, main()

write down the file path like this:

String fileName = args[0]; // assuming the user has provided arguments

try (InputStream in = Files.newInputStream(Paths.get(fileName));
        BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
        String line;
        while ((line = reader.readLine()) != null) {
            string1 = string1 + " " + line;
        }
}

      

0


source


You can put files in your application's resource folder and use them like this:

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("test.txt").getFile());

      



If your resources have a whole batch of files, you can use getResources (""); to view all resources in your application and select those which are your text files. those. put them in a special folder, so it would be easier to distinguish between them.

0


source


There have been many good suggestions, but no one mentioned environment variables or system properties. You can define an environment variable for your OS and get it like this:

String configPath = System.getenv("CONFIG_PATH");

      

Or you can pass a system variable to the application when it starts:

java -jar app.jar -DconfigPath="/home/..."

      

Then use it in your code:

String configPath = System.getProperty("configPath");

      

0


source







All Articles