Sunday 15 April 2012

java - getResourceStream unable to load newly created file in eclipse -


I'm dynamically creating a file using file IO,

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

and getClass () Trying to load sample.txt using GetResourceAsStream ("sample.txt"), and it is unable to load the file, but if I refresh this project, in the eclipse, it loads the file Is able to get files, rsosstream cache to get files?, Do i need to remember how to make files dynamically and load them and do some actions on them? Land?

Code InputStream = getClass (). GetClassLoader (). GetResourceAsStream ("sample.txt"); // absolute path

PS: The newly created file was added to the created path

When you run your app, you create that file in your source tree (usually / src). But your bin folder is on square path (usually / bin) because you create files outside of Eclipse (in any other Java process), this happens when you refresh your workspace 1) It is assumed in the source folder And 2) Then the builder makes it copy to bin folder.

getResourceAsStream () should be used for something that is guaranteed to be on a classpath, for example a stable resource in the source tree. For dynamically generated files, you must find or pass in another location and do not use getResourceAsStream ().

No comments:

Post a Comment