0

I am using JSP on NetBeans.

In the java code, I am trying to read data from a file. First I open the file by specifying its path.

Because my code runs on a server (GlassFish), I would like to have my file path independent of the machine where it runs. Therefore, I want to start the path with the folder name that contains the file which is saved on the root of the project directory. I tried so hard to achieve that but I couldn't.

I read online and I found this way, but it still doesn't work:

<%

  //building the tree here.
GraphBuilder tree = new GraphBuilder("${pageContext.request.contextPath}\\src\\java\\Database\\OptimizedFullTermFile.pad");

%>

Can anyone help? Thank you.

Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83

2 Answers2

1

Just read it from the classpath. Given that you're using the typical src folder representing the Java source code (the Java package/class structure), I assume that the file OptimizedFullTermFile.pad is placed in the Java package java.Database (eek, a capital in package name? lowercase it all). In that case, it's already in the classpath and thus you can just get it straight from there as follows:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("java/Database/OptimizedFullTermFile.pad");
// ...

As to your failed attempt: the EL ${pageContext.request.contextPath} isn't ever going to work in a scriptlet. Even if it did, it's not the right thing, it returns the context path in the webapp URL which is absolutely not part of the local disk file system path, let alone the classpath. Using scriptlets is strongly discouraged since a decade, by the way.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I would like to use File instead of InputStream. How can I convert your way to File? Thanks. – Traveling Salesman Dec 18 '12 at 12:04
  • I strongly recommend to not do that. Such an API which requires a `File` as argument is heavily flawed. But if you really can't do otherwise, just create a temporary file by `File#createTempFile()` and write to it. Then, use that temporary file. – BalusC Dec 18 '12 at 12:13
  • I did this: InputStream input; input = Thread.currentThread().getContextClassLoader().getResourceAsStream("java\\Database\\OptimizedFullTermFile.pad"); InputStreamReader isReader = new InputStreamReader(input); BufferedReader lineReader = new BufferedReader(isReader); then I started to read the file line by line. It still doesn't work. – Traveling Salesman Dec 18 '12 at 12:39
  • Apparently the file is not there where your code expects it is. Where exactly in the built WAR file is the file located? My answer assumes (based on your own attempt in the question) that it's inside `/WEB-INF/classes/java/Database/OptimizedFullTermFile.pad`. If it isn't there, but actually elsewhere, then you need to fix the path in the code accordingly to match it. Further, you should not be using back slashes but forward slashes. The Java classpath is not a Windows based disk file system or something. – BalusC Dec 18 '12 at 18:39
  • When I wrote this: input = Thread.currentThread().getContextClassLoader().getResourceAsStream ("Database/SmallOptimizedFullTermFile.pad"); it worked successfully! my file is in the following full path: "C:\Users\student\Documents\NetBeansProjects\geneOntology\geneOntology\src\java\Database\OptimizedFullTermFile.pad". Thanks a lot for your help. – Traveling Salesman Dec 18 '12 at 19:07
1

You can try the following code to get your directory file in project from the following code.

this.getServletContext().getRealPath("")+[your file path in project web directory]
sumit sharma
  • 1,067
  • 8
  • 24