Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

locating file in a classpath

Tags:

java

I'm trying to read in file content, ex :

public void myMethod(){
     FileInputStream fstream = new FileInputStream(fileLocation);
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
     String strLine;
     while ((strLine = br.readLine()) != null) {
....
....
.....
end while 
end method

And I have at the begining of the class body private String fileLocation; and at the end of a class I have a getter and setter for it. Now I'm trying inject this file location from spring inside bean from this class and I specify the init-method of this class. But I get error cannot find the specified file as if its not on a classpath but it is inside war file? I'm building the project with maven and I put file in src/main/resources This is the error I get when trying to read file :

Error: src\main\resources\ids.txt (The system cannot find the path specified)

That is when I tried this :

FileInputStream fstream = new FileInputStream("src\\main\\resources\\ids.txt");

how to reference the properly from the classpath?

EDIT

When I edit my code according to @BalusC solution , here is how it looks but I still get null error :

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
   InputStream input = classLoader.getResourceAsStream("src/main/resources/ids.txt");
   BufferedReader br = new BufferedReader(new InputStreamReader(input));
   String strLine;
 while ((strLine = br.readLine()) != null) {
    ....
    ....
    .....
    end while 
    end method
like image 861
Gandalf StormCrow Avatar asked Apr 19 '10 11:04

Gandalf StormCrow


People also ask

How do I find the location of a file in Java?

In Java, for NIO Path, we can use path. toAbsolutePath() to get the file path; For legacy IO File, we can use file. getAbsolutePath() to get the file path.


1 Answers

The Java IO API relies on the local disk file system, not on the classpath. Besides, using relative paths in Java IO stuff is recipe for portability trouble, don't rely on it. To allocate resources in the classpath you would normally use ClassLoader#getResource() or ClassLoader#getResourceAsStream().

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("src/main/resources/ids.txt");

That said, you don't need that DataInputStream line. You're actually not taking any benefit from it.

Update: if that doesn't work, then either the resource name is simply invalid or the file is actually not there in the classpath where you expect it to be. My cents on that the src folder is actually the root of the classpath and not part of a package. Remove it from the name.

Update 2: to get all root disk file system paths which are covered by the runtime classpath do:

for (URL root : Collections.list(Thread.currentThread().getContextClassLoader().getResources(""))) {
    System.out.println(root);
}

The resource name needs to be relative to either of them. That it is been placed in /WEB-INF/classes during the build is normal. It is covered by the classpath. Your problem lies somewhere else. Are you sure that the resource name is correct? Are you sure that you're running the code you think you are running?

like image 171
BalusC Avatar answered Oct 22 '22 00:10

BalusC