3

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?

Daryl Bennett
  • 462
  • 10
  • 22
Will R.
  • 237
  • 2
  • 4
  • 10
  • Do you want to put them in the classpath ? – AllTooSir May 17 '13 at 16:53
  • If that's what's needed to make the files readable to the method in question, then yes. (Sorry again, but I'm very new to this, so I don't know if it's necessary.) – Will R. May 17 '13 at 16:59

4 Answers4

2

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.

dharam
  • 7,882
  • 15
  • 65
  • 93
1

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.

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
0

Add the files in the projects classpath.(you can find the class path of the project by right click the project in eclipse->Build Path->configure build path)

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
-1

I guess you want an internal .txt file.

Package Explorer => Right Click at your project => New => File . Then text a file name and Finish it.

The path in your code should look like this:

Scanner diskScanner = new Scanner(new File("YourFile"));
slanecek
  • 808
  • 1
  • 8
  • 24