Java handles internationalization and reads the properties file code snippet

Source: Internet
Author: User

1. Java Processing internationalization fragments


Static

{

Logger.log (Level.info, "Start loading language environment");

Tacit consent to Chinese

locale = new locale (Utilsconstants.utils_language,

Utilsconstants.utils_country);

Messages = Resourcebundle.getbundle (Configfilepath.getpackagespath ()

+ utilsconstants.utils_base_res, locale);

}

public string GetMessage (string target)

{

Try

{

TODO auto-generated Method Stub

return messages.getstring (target);

} catch (MissingResourceException e)

{

Logger.log (Level.severe,

Messages.getstring ("Utils.message.log.NoKeyInFile")

+ "[" + Target + "]");

Return "";

}

}

2. Java read-write properties file

/**

* Initialize Load

*/

Static

{


Logger.log (Level.info, GetMessage ("Utils.message.log.startLoadFile"));


InputStream in = null;

Build Property File path

String FilePath = configfilepath.getfilepaths () + fileName;


Load Properties File

Try

{

in = new Bufferedinputstream (new FileInputStream (FilePath));


Properties.load (in);

Logger.log (Level.info, GetMessage ("Utils.message.log.endLoadFile"));


} catch (FileNotFoundException e)

{

TODO auto-generated Catch block

Logger.log (Level.severe,

GetMessage ("Utils.message.log.createFileStreamFail"), e);

} catch (IOException e)

{

TODO auto-generated Catch block

Logger.log (Level.severe,

GetMessage ("Utils.message.log.loadPropertyFileFail"), e);

} finally

{


Try

{

In.close ();

} catch (IOException e)

{

TODO auto-generated Catch block

Logger.log (Level.severe,

GetMessage ("Utils.message.log.closeFileStreamFail"), e);

}

}

}


@Override

public string ReadProperty (string key)

{

TODO auto-generated Method Stub


if (null = = Properties.getproperty (key))

{

Logger.log (Level.severe,

GetMessage ("Utils.message.log.propertyIsNull"));

}

return Properties.getproperty (key);

}


@Override

public boolean WriteProperty (string key, String value)

{

TODO auto-generated Method Stub


Properties.setproperty (key, value);

String FilePath = configfilepath.getfilepaths () + fileName;


OutputStream out = null;

Save Properties File

Try

{

out = new Bufferedoutputstream (new FileOutputStream (FilePath));

Properties.store (Out, "");

} catch (FileNotFoundException e)

{

TODO auto-generated Catch block

Logger.log (Level.severe,

GetMessage ("Utils.message.log.createFileStreamFail"), e);

} catch (IOException e)

{

TODO auto-generated Catch block

Logger.log (Level.severe,

GetMessage ("Utils.message.log.stroePropertyFileFail"), e);

} finally

{

Try

{

Out.close ();

} catch (IOException e)

{

TODO auto-generated Catch block

Logger.log (Level.severe,

GetMessage ("Utils.message.log.closeFileStreamFail"), e);

}

}


return false;

}


Java handles internationalization and reads the properties file code snippet

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.