[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.newcomer] Re: Problem with finding a text file in Eclipse workspace

Stefan Zugal wrote:

Hi,

consider the output of following statement (it will tell you where the local root is):

System.out.println(new File("").getAbsoluteFile());


Anyway, if you want to take advantage of the Eclipse workspace, you will need the org.eclipse.core.resources plugin:

ResourcesPlugin.getWorkspace()

will return the workspace. Eclipse help should give you more information about how to use the workspace.


If you want to access files of your plugin, you may use:
Bundle#getResource(String name)

HTH,
Stefan

This is the procedure to follow if you are writing an Eclipse plugin. To me, it sounds more like you are writing a Java application. In this case, you should look into the Class.getResource() and Class.getResourceAsStream() methods. These will search for the resource on your classpath. My guess is that something along the lines of: this.getClass().getResourceAsStream("/temp.txt") should work. Read the Javadoc for both methods and pay attention to the discussion about leading '/' characters for pathing purposes.


www schrieb:
Hi,

My Java program needs a local text file, temp.txt. My Java program is located in Eclipse workspace, Test_Workspace. One easy way for the Java program to find and use the text file is to hard-code the absolute path to the file(i am using linux). But I don't like it. I hope to take advantage of Eclipse workspace.

temp.txt has been added to the workspace, Test_Workspace, shown in Package Explorer. But the following line cannot find the file:

BufferedReader inputStream = new BufferedReader(new FileReader("temp.txt"));

What is wrong with me?

Thank you very much.