简体   繁体   中英

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

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( ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM