Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getResourceAsStream - What encoding is it read as?

Tags:

I am using getResourceAsStream to access a local file. What encoding does it assume the file is?

like image 516
Casebash Avatar asked Apr 08 '11 04:04

Casebash


People also ask

What does getResourceAsStream return?

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.

What is getClassLoader () getResourceAsStream?

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).

When using class getResourceAsStream Where will the resource be searched?

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.

How do you get path from getResourceAsStream?

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 / .


1 Answers

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.

like image 80
ColinD Avatar answered Sep 19 '22 15:09

ColinD