user11011990
user11011990

Reputation: 76

getResourceStream unable to load newly created file in eclipse

I am creating a file dynamically using File IO ,

FileOutputStream fos = new FileOutputStream("sample.txt");

and trying to load sample.txt using getClass().getResourceAsStream("sample.txt") in another class, and it is unable to load the file, whereas if i refresh the project in eclipse ,it is able to load the file, does getResourceStream cache the files?, What i need is to create files dynamically and load them and perform some operations on them, Am I missing something?

Code InputStream is = getClass().getClassLoader().getResourceAsStream("sample.txt");//absolute path

PS :Added the newly created file in build path

Upvotes: 1

Views: 149

Answers (1)

Paul Webster
Paul Webster

Reputation: 10654

When you run your app, you create that file in your source tree (usually /src). But your bin folder is what is on the classpath, (usually /bin). Because you create the file outside of eclipse (in another java process) it's only when you refresh your workspace that 1) it shows up in the source folder to eclipse and 2) then the builder copies it over to the bin folder.

getResourceAsStream() is meant to be used for something that is guaranteed to be on the classpath, a static resource in the source tree for example. For dynamically generated files, you need to find or pass in some other location and not use getResourceAsStream().

Upvotes: 1

Related Questions