Unicode in Jar Resources

I have a Unicode text file (UTF-8 without specification) inside a jar that loads as a resource.

URL resource = MyClass.class.getResource("datafile.csv");
InputStream stream = resource.openStream();
BufferedReader reader = new BufferedReader(
    new InputStreamReader(stream, Charset.forName("UTF-8")));

This works fine on Windows, but on Linux it seems like it is not reading the file correctly - the shock symbols go out. I know that different machines may have different default encodings, but I give it the correct encoding. Why not use it?

+3
source share
2 answers

Part of the reading looks right, I use it all the time on Linux.

I suspect that you used the default encoding somewhere when you export text to a web page. Due to the different default encoding on Linux and Windows, you saw a different result.

, , - ,

PrintWriter out = response.getWriter();
out.println(text);

UTF-8, ,

 response.setContentType("text/html; charset=UTF-8");
 out = new PrintWriter(
    new OutputStreamWriter(response.getOutputStream(), "UTF-8"), true);
 out.println(text);
+2
+1

Source: https://habr.com/ru/post/1715852/


All Articles