0

I want to read a file from directory.File is in root directory. If i use path as E:\Java\Netbeans_practice\project_141\Description.txt then it works fine.But when i wanted to use path as the file name or within a defined folder as Info\Description.txt , it gives error (java.io.FileNotFoundException: Description.txt (The system cannot find the file specified)). Actually i don't want to use the path name before project directory (ex: E:\Java\Netbeans_practice\project_141).I have searched a lot but unable to solve.Please help me. Here is my portion of code :

      Scanner in = new Scanner(new FileReader("Description.txt");
      while(in.hasNextLine()){
                                out.print("* "+in.nextLine()+"<br>");
                            }
Patriotic
  • 2,103
  • 4
  • 26
  • 36
  • So what's the relation between code and question detail with JSP?(you have tagged it) – void Jan 25 '15 at 21:31
  • Where is the `Info` directory located? In the code example you provided, it is expecting it to be in the root directory. Does it exist? – Drew Kennedy Jan 25 '15 at 21:33
  • Is this run in Tomcat servlet(jsp) engine? Webapps have a working folder in $tomcat/bin/ folder so use it as a base, then "../../" backtrack accordingly. Another trick is to use servletContext.getRealPath("/") to read a folder where webapp is located, use it as a base for locating files. – Whome Jan 25 '15 at 21:37
  • i also tried creating Info folder and Description.txt file inside in it and changing FileReader("Info\Description.txt") but it didn't work.My target is to read the file from anywhere inside root directory not using the path url which started before project directory . – Patriotic Jan 25 '15 at 21:40

1 Answers1

0

When you deploy your web app, only the contents inside the "WebContent" will be deployed. You can verify this by going to (assuming you are using tomcat in your eclipse):

projectworkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\<contextName>

So you may wanan copy your "Description.txt" file into "/WEB-INF" (for security sake) directory. Then you should be able to access it:

File file = new File(getServletContext().getRealPath("/WEB-INF/Description.txt"));

Update:

String path="/WEB-INF/Description.txt";
InputStream inputStream = this.getServletConfig().getServletContext().getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Sas
  • 2,473
  • 6
  • 30
  • 47
  • Thank you for reply...ServletContext().getRealPath() works fine locally but when i am going to deploy it in openshift , getRealPath method returns null...i tried using getContextPath() , getResource() etc but still i am unable to generate the url of my text file and not getting the context path. @Sas @ Whome – Patriotic Jan 26 '15 at 20:18
  • Try the solution provided [here](http://stackoverflow.com/questions/536228/why-does-getrealpath-return-null-when-deployed-with-a-war-file). – Sas Jan 26 '15 at 20:31
  • Still now in unable to solve the issue ..... :( And getting error: java.io.FileNotFoundException: null/Description.txt (No such file or directory) – Patriotic Jan 27 '15 at 19:39