Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: how/where to include a text file in a Java project?

Tags:

java

eclipse

I'm using Eclipse (SDK v4.2.2) to develop a Java project (Java SE, v1.6) that currently reads information from external .txt files as part of methods used many times in a single pass. I would like to include these files in my project, making them "native" to make the project independent of external files. I don't know where to add the files into the project or how to add them so they can easily be used by the appropriate method.

Searching on Google has not turned up any solid guidance, nor have I found any similar questions on this site. If someone knows how to do add files and where they should go, I'd greatly appreciate any advice or even a point in the right direction. Also, if any additional information about the code or the .txt files is required, I'll be happy to provide as much detail as possible.

UPDATE 5/20/2013: I've managed to get the text files into the classpath; they're located in a package under a folder called 'resc' (per dharam's advice), which is on the same classpath level as the 'src' folder in which my code is packaged. Now I just need to figure out how to get my code to read these files properly. Specifically, I want to read a selected file into a two-dimensional array, reading line-by-line and splitting each line by a delimiter. Prior to packaging the files directly within the workspace, I used a BufferedReader to do this:

public static List<String[]> fileRead(String d) {
    // Initialize File 'f' with path completed by passed-in String 'd'.
    File f = new File("<incomplete directory path goes here>" + d);

    // Initialize some variables to be used shortly.
    String s = null;
    List<String> a = new ArrayList<String>();
    List<String[]> l = new ArrayList<String[]>();

    try {
        // Use new BufferedReader 'in' to read in 'f'.
        BufferedReader in = new BufferedReader(new FileReader(f));

        // Read the first line into String 's'. 
        s = in.readLine();

        // So long as 's' is NOT null...
        while(s != null) {
            // Split the current line, using semi-colons as delimiters, and store in 'a'.
            // Convert 'a' to array 'aSplit', then add 'aSplit' to 'l'.
            a = Arrays.asList(s.split("\\s*;\\s*"));
            String[] aSplit = a.toArray(new String[2]);
            l.add(aSplit);

            // Read next line of 'f'.
            s = in.readLine();
        }

        // Once finished, close 'in'.
        in.close();
    } catch (IOException e) {
        // If problems occur during 'try' code, catch exception and include StackTrace.
        e.printStackTrace();
    }

    // Return value of 'l'.
    return l;
}

If I decide to use the methods described in the link provided by Pangea (using getResourceAsStream to read in the file as an InputStream), I'm not sure how I would be able to achieve the same results. Would someone be able to help me find a solution on this same question, or should I ask about that issue into a different question to prevent headaches?

like image 379
Will R. Avatar asked Jan 22 '26 22:01

Will R.


2 Answers

You can put them anywhere you wish, but depends on what you want to achieve through putting the file.

A general practice is to create a folder with name resc/resource and put files in it. Include the folder in classpath.

like image 191
dharam Avatar answered Jan 24 '26 15:01

dharam


You can store the files within a java package and read them as classpath resources. For e.g. you can add the text files to a java package say com.foo and use this thread to know how to read them: How to really read text file from classpath in Java

This way they are independent of the environment and are co-packaged with code itself.

like image 31
Aravind Yarram Avatar answered Jan 24 '26 15:01

Aravind Yarram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!