How to fix Chinese character garbling in Java output?
The problem of Chinese characters displaying as garbled text in Java output is usually caused by inconsistent encoding or issues with the output stream settings. Here are some possible solutions:
- Print the output to the console.
System.setProperty("file.encoding", "UTF-8");
- A writer that writes output to a stream.
OutputStream outputStream = new FileOutputStream("output.txt");
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
- Can you please translate this into English natively?
System.out.println("\u4F60\u597D");
This ensures that Chinese characters are output in Unicode encoding, avoiding encoding issues.
I hope these solutions will help you solve the problem of garbled Chinese characters in Java output.