Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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: srcmain esourcesids.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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
118 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...