I am using getResourceAsStream to access a local file. What encoding does it assume the file is?
The getResourceAsStream method returns an InputStream for the specified resource or null if it does not find the resource. The getResource method finds a resource with the specified name. It returns a URL to the resource or null if it does not find the resource.
getClassLoader(). getResourceAsStream("abc. txt") and find that it searchs the resource in all jar file and zip file in class path. Thats correct when you work only with a single ClassLoader (most non-OSGi/ non-modular environments).
The getResourceAsStream() method of java. lang. Class class is used to get the resource with the specified resource of this class. The method returns the specified resource of this class in the form of InputStream object.
getResourceAsStream , send the absolute path from package root, but omitting the first / . If you use Class. getResourceAsStream , send either a path relative the the current Class object (and the method will take the package into account), or send the absolute path from package root, starting with a / .
InputStreams don't have encodings. They're just streams of bytes. Readers are for text with an encoding. You can create a Reader with a specific charset from an InputStream like this:
Reader reader = new InputStreamReader(inputStream, "UTF-8");
If you're using a charset that's guaranteed to be supported on all Java platforms like UTF-8, you can avoid having to deal with impossible UnsupportedEncodingExceptions by using a constant from Guava's Charsets class like Charsets.UTF_8.
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