0

Here it is my folder project
enter image description here

I would like to read the file book-form.html which is in the directory web of my project and put it in a String.

This is how I call my function 'getFileContent':

String content = getFileContent("web/book-form.html");

And this is the function:

public String getFileContent(String filePath){
        String line, content = new String();
        try {
                File file = new File(filePath);
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                while((line = br.readLine()) != null){
                        content += line;
                }
                br.close();
            fr.close();
        } catch(IOException e){
                System.out.println(e.getMessage());
        }

        return content;
    }

My problem is that netbeans tell me that it cannot find my file book-form.html

Any ideas ?

2 Answers2

1

File path to resource in our war/WEB-INF folder?

Also you should close stream in a final block or use try-with-resource if you use jdk 7+

Community
  • 1
  • 1
jjd
  • 2,158
  • 2
  • 18
  • 31
0

I find the way to do it:

Basically the program is in the main folder of Glassfish, so it's needed to put the entire path of your file from the root of your system to allow the program to find your file.

  • What if you deploy your application on a different server instance? – jjd Apr 08 '15 at 20:15
  • Basically the root folder of the program is the 'web' folder and not the project's folder. So if you deploy your application on a different server I suppose you need to put all your files in the 'web' folder which will be the folder of your server. – Florian Malapel Apr 10 '15 at 13:35
  • 'to put the entire path of your file from the root of your system'. Once you move your application to a different AS instance, your application most probably will stop working because absolute paths you used would be incorrect(unless the AS is deployed at the same folder as the original one). The solution you suggested for your own problem is not correct (even if it works for your particular environment). It just does not make any sense to implement it this way except for some testing purpose where it is still better to use appropriate method of ServletContext or Class or ClassLoader – jjd Apr 10 '15 at 15:28