How can I solve the problem of reading the propertiesfile from Java? (native2ascii.exe and other tools are not used)
Principle: Transcoding
Key code: New String (temp. getbytes ("ISO-8859-1"), "GBK ")
In fact, it is very simple, that is, simple transcoding. The following provides the source code (two readproperties. Java and logsystem. properties) and usage:
Readproperties. Java
Import java. util. properties;
Import java. Io. unsupportedencodingexception;
Import java. Io. fileinputstream;
Import java. Io .*;
Public class readproperties {
Private Static Properties props = new properties ();
Private Static fileinputstream FCM;
Private Static string propertiesname = "logsystem. properties"; // The Name Of The properties file to be read, which is in the current directory by default.
Private Static void initializationproperties (){
Try {
FS = new fileinputstream (propertiesname );
Props. Load (FS );
FCM. Close ();
} Catch (filenotfoundexception ex ){
System. Out. println ("logsystem. Tools. readproperties: the system cannot find the file->" +
Propertiesname );
} Catch (ioexception ex ){
Ex. printstacktrace ();
} Finally {
If (FS! = NULL ){
Try {
FCM. Close ();
} Catch (ioexception ex1 ){
Ex1.printstacktrace ();
}
}
}
}
/**
* Obtain the configuration value based on the configuration item
* Compatible with GBK encoding and supports Chinese Characters
* @ Param key string -- configuration item
* @ Return string -- configuration value
*/
Public static string getpropertybykey (string key ){
Initializationproperties ();
String temp = props. getproperty (key );
String sp = "";
If (temp! = NULL ){
Try {
SP = new string (temp. getbytes ("ISO-8859-1"), "GBK ");
} Catch (unsupportedencodingexception e ){
E. printstacktrace ();
}
}
Props. Clear ();
Return sp;
}
Public static void main (string [] ARGs ){
System. Out. println (readproperties. getpropertybykey ("clientlogfilename"); // is it easy to use?
}
}
Clientlogfilename = I am not a person
Console printing:
I am not alone
Hope to help you!