2

In a JSP project I am reading a file from directory. If i give the full path then i can easily read the file

BufferedReader br = new BufferedReader(new FileReader("C:\\ProjectFolderName\\files\\BB.key"));

but i don't want to write the full path instead i just want to give the folder name which contains the file, like bellow.

BufferedReader br = new BufferedReader(new FileReader("\\files\\BB.key"));

How to do this?

String currentDirectory = new File("").getAbsolutePath();
System.out.println(currentDirectory);
BufferedReader br = new BufferedReader(new FileReader(currentDirectory + "\\files\\BB.key"));

I tried the above still cant read from file

the print line gives the following output

INFO: C:\Program Files\NetBeans 7.3

LynAs
  • 6,407
  • 14
  • 48
  • 83

4 Answers4

4

Use

File file = request.getServletContext().getRealPath("/files/BB.key");

This translates URL paths relative (hence '/') from the web contents directory to a file system File.

For a portable web application, and knowing the file is in Windows Latin-1, explicitly state the encoding, otherwise the default OS encoding of the hoster is given.

BufferedReader br = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "Windows-1252"));

If the file is stored as resource, under /WEB-INF/classes/ you may also use

BufferedReader br = new BufferedReader(new InputStreamReader(
        getClass().getResourceAsStream("/files/BB.key"), "Windows-1252"));

In that case the file would reside under /WEB-INF/classes/files/BB.key.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • What do we do if this is not a servlet, but just any other java class, i.e. there is no getServletContext method and all that ? – MasterJoe Dec 01 '17 at 01:22
3

It is better to read the file as a classpath resource rather than a file system resource. This helps you to avoid hard-coding or parameterizing environment specific folder. Follow this post Reading file from classpath location for "current project"

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
2

Add this:

private static String currentDirectory = new File("").getAbsolutePath();

and change your BufferedReader to:

BufferedReader br = new BufferedReader(new FileReader(currentDirectory + "\\files\\BB.key"));

currentDirectory will contain whatever path the project directory is in (where you're running the program from).

Aaron
  • 992
  • 3
  • 15
  • 33
0

If you reference a file by relative path (new FileReader("files/BB.key")), then it will resolve against the current work directory when executing your program.

What exactly do you try to achieve?

If you want to package a file with your program and then access this programmatically, put it on the classpath and load it as ressource with one of the Class.getResource... methods.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • the idea is i upload a file in files directory in my project folder and then read lines from that uploaded file – LynAs May 24 '13 at 23:53
  • You "upload" it during runtime or before delivery of your project and deliver it together with your project? If during runtime, how do you "upload" the file, what is the usecase? – Vampire May 25 '13 at 11:36