I have a folder structure like Project
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());
}
}
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.
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 .
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().
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With