Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the Standard Out to "UTF-8" in Java

Tags:

java

I download a file from a website using a Java program and the header looks like below

Content-Disposition attachment;filename="Textkürzung.asc";

There is no encoding specified

What I do is after downloading I pass the name of the file to another application for further processing. I use

System.out.println(filename);

In the standard out the string is printed as Textk³rzung.asc

How can I change the Standard Out to "UTF-8" in Java?

I tried to encode to "UTF-8" and the content is still the same

Update:

I was able to fix this without any code change. In the place where I call this my jar file from the other application, i did the following

java -DFile.Encoding=UTF-8 -jar ....

This seem to have fixed the issue

thank you all for your support

like image 993
KK99 Avatar asked Feb 17 '15 17:02

KK99


People also ask

How do I save a Java file as UTF-8?

In Java, the OutputStreamWriter accepts a charset to encode the character streams into byte streams. We can pass a StandardCharsets. UTF_8 into the OutputStreamWriter constructor to write data to a UTF-8 file.

How do I change character encoding in Java?

java -Dfile. encoding="UTF-8" HelloWorld, we can specify UTF-8 charset. Method 2: Specifying the environment variable “JAVA_TOOLS_OPTIONS.” In case we start JVM starts up using some scripts and tools, the default charset can be set using the environment variable JAVA_TOOL_OPTIONS to -Dfile.


1 Answers

The default encoding of System.out is the operating system default. On international versions of Windows this is usually the windows-1252 codepage. If you're running your code on the command line, that is also the encoding the terminal expects, so special characters are displayed correctly. But if you are running the code some other way, or sending the output to a file or another program, it might be expecting a different encoding. In your case, apparently, UTF-8.

You can actually change the encoding of System.out by replacing it:

try {
    System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8"));
} catch (UnsupportedEncodingException e) {
    throw new InternalError("VM does not support mandatory encoding UTF-8");
}

This works for cases where using a new PrintStream is not an option, for instance because the output is coming from library code which you cannot change, and where you have no control over system properties, or where changing the default encoding of all files is not appropriate.

like image 59
Pepijn Schmitz Avatar answered Nov 08 '22 01:11

Pepijn Schmitz