kevin ternet kevin ternet - 1 year ago 89
Java Question

How my bean can read a file

I' need help to open a file using Eclipse. indeed, I don't know where to locate my file in Eclipse file tree to avoid raising this exception :

** wages.xls **
at this line :
FileInputStream file = new FileInputStream("wages.xls");

Answer Source

You need to add this file to a folder located in your classpath, usually named resources. You can then access the content of this file through the Classloader of your classes :

class Test {
    public static void main(String[] args) {
        InputStream myFileContent = Test.class.getResourceAsStream("/directory/file");

This would work with a project with the following file tree :

  • projectRoot
    • src
    • resources
      • directory
        • file

And a classpath including both the src and resources directories.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download