开发者

locating file in a classpath

开发者 https://www.devze.com 2022-12-27 21:42 出处:网络
I\'m trying to read in file content, ex : public void myMethod(){ FileInputStream fstream = new FileInputStream(fileLocation);

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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号