Brad
Brad

Reputation: 9223

Read a file in java from a linked folder in eclipse

So I'm attempting to read a file in an Eclipse Java project. The resource itself is in a linked folder that I've added using:

New -> Folder. Advanced >> Link folder etc.

I couldn't open the file so I wrote a simple method to find out what I could access:

public static void main(String[] args)
{
    File folder = new File(".");
    String[] listOfFiles = folder.list();

    for (int i = 0; i < listOfFiles.length; i++)
    {
        System.out.println("file: " + listOfFiles[i]);
    }
}

This leaves me with the following output:

file: .classpath
file: .project
file: bin

So how do I open files in Eclipse from a linked folder location?

If I can't, what's the purpose of Linked Folders?

Thanks in advance.

Upvotes: 1

Views: 2890

Answers (2)

Damien
Damien

Reputation: 2254

At runtime, your main method is not aware of any Eclipse configuration. Running it from Eclipse gives the same result than running it from command line. So, since the linked resources are not physically present in the directory of your project, the ouput you get is the expected one.

Linked resources can be useful for anything controlled by Eclipse (mainly source or output directories).

Upvotes: 2

MeBigFatGuy
MeBigFatGuy

Reputation: 28588

The only thing i can see that works is if you edit your debug configuration's classpath user entries to click the Advanced... button and click add folders, and select that folder, then if you want to get that file you can do

InputStream is = Someclass.class.getResourceAsStream("filename_in_your_linked_folder")

so i suppose i don't think it's really of much use for what you want.

Upvotes: 1

Related Questions