Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to read file using getResourceAsStream

Tags:

java

I have a folder structure like Project

  • src
  • --TestMain.java
  • bin
  • --TestMain.class
  • resources
  • --test.txt

As the whole project will be packaged into a jar file, I have to read a file from resources using getResourceAsStream. Although I have read through all questions about getResourceAsStream, I still can't get this working. Could anyone help? Thank you!

public class TestMain {

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub

InputStream stream = TestMain.class.getResourceAsStream("\resources\test.txt");
    System.out.println(stream);
    BufferedReader bufRead = new BufferedReader(new InputStreamReader(stream));
    StringBuilder builder = new StringBuilder();
    String line=null;
    while((line=bufRead.readLine())!=null){
        builder.append(line).append("\n");
    }
    System.out.println(builder.toString());

}
}
like image 743
user3321400 Avatar asked Mar 12 '14 05:03

user3321400


People also ask

How do I read a file from SRC main resources?

Using Java getResourceAsStream() This is an example of using getResourceAsStream method to read a file from src/main/resources directory. First, we are using the getResourceAsStream method to create an instance of InputStream. Next, we create an instance of InputStreamReader for the input stream.

How do I view the resources folder in a jar file?

This works when running inside and outside of a Jar file. PathMatchingResourcePatternResolver r = new PathMatchingResourcePatternResolver(); Resource[] resources = r. getResources("/myfolder/*"); Then you can access the data using getInputStream and the filename from getFilename .

How do I read a resource folder in Java 8?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().


2 Answers

Basically, there are 2 different methods: ClassLoader.getResourceAsStream() and Class.getResourceAsStream(). These two methods will locate the resource differently.

In Class.getResourceAsStream(path), the path is interpreted as a path local to the package of the class you are calling it from. For example calling, String.getResourceAsStream("file.txt") will look for a file in your classpath at the following location: "java/lang/file.txt". If your path starts with a /, then it will be considered an absolute path, and will start searching from the root of the classpath. So calling String.getResourceAsStream("/myfile.txt") will look at the following location in your class path ./file.txt.

ClassLoader.getResourceAsStream(path) will consider all paths to be absolute paths. So calling String.getClassLoader().getResourceAsString("myfile.txt") and String.getClassLoader().getResourceAsString("/file.txt") will both look for a file in your classpath at the following location: ./file.txt.

Every time the location, it could be a location in your filesystem itself, or inside the corresponding jar file, depending on the Class and/or ClassLoader you are loading the resource from.

IF you are loading the class from an Application Server, so your should use Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) instead of this.getClass().getClassLoader().getResourceAsStream(fileName). this.getClass().getResourceAsStream() will also work.

like image 108
Maheshbabu Jammula Avatar answered Oct 23 '22 08:10

Maheshbabu Jammula


It seems the folder 'resources' is in your classpath, it is not need while getting resources under it,try below

InputStream stream = TestMain.class.getResourceAsStream("test.txt");
like image 43
Safrain Avatar answered Oct 23 '22 09:10

Safrain