How can I solve the problem of reading the propertiesfile from Java? (native2ascii.exe and other tools are not used)

Source: Internet
Author: User
Tags key string

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!


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.