Chinese garbled solution when Java reads the properties configuration file
Transferred from: http://pig345.iteye.com/blog/725974
Encountered the use of Java.util.Properties reading Chinese content (UTF-8 format) of the configuration file, the occurrence of Chinese garbled phenomenon,
Java code
- properties prop=new properties ();
- Prop.load (Client. Class. getClassLoader (). getResourceAsStream ("config.properties"));
Habitually Google a bit, most of the online article is to let everyone use native2ascii.exe conversion Such solution, at first, almost be confused, thought can only use such mealy method ...
But the point is, it's too round! If every time with Native2ascii.exe to convert Chinese into \uxxxx\uxxxx such, trouble first not to say, after the conversion of the file is completely unreadable!!! This is basically intolerable!
(although it can also be converted back with Native2ascii.exe, but again, trouble!) )
Calm down, suddenly remembered, or beginner Java saw, java.io package in the difference between Reader/writer and stream.
(years old, the details forget, presumably:reader/write is processing encoded text, and Inputstream/outputstream only the data as 2 binary stream)
The right solution
Java code
- properties prop=new properties ();
- Prop.load (new InputStreamReader (Client). Class. getClassLoader (). getResourceAsStream ("Config.properties"), "UTF-8"));