Java Properties usage and format definition, javaproperties
Java. util. PropertiesExtendsHashtable <Object, Object>
A text resource tool that allows you to conveniently read key-value pairs.
Overview of common methods
Initialization object
New Properties ();
Load files
Load (Reader/InputStream );
LoadFromXML (InputStream );
Get key value
GetProperty (String key): String
GetProperty (String key, String defaultValue): String
Insert key value
SetProperty (String key, String value );
Save files
Store (OutputStream out, String comments );
Store (Writer writer, String comments );
StoreToXML (OutputStream OS, String comment );
StoreToXML (OutputStream OS, String comment, String encoding)
Print and list all key-value pairs (for development and testing)
List (PrintStream/PrintWriter );
Obtain a key table
PropertyNames (): Enumeration // equivalent to Iterator
StringPropertyNames (): Set <String>
Properties text format
- To! Or the line starting with # is used as the comment line.
# This is a comment
! This is a comment
- One key-value pair per row
- Key-value pairs are separated by four types of characters: [=,:, space, tab]
Key: value
Key = value
Key value
Key value
- [=,:] As a key character, you need to insert an escape character \
# The Key is "key1: key2"
Key1 \: key2 = value
# The Key is "key1 = key2"
Key1 \ = key2 = value
- Ignore all spaces and tabs that are not actually meaningful
# All the following key-value pairs have the same format meaning
Key = value
Key = value
Key: value
- When the value is too long, it supports branch writing and inserts an escape character \ at the end of the value \
# All spaces directly between the Escape Character and the next valid value will be ignored
Key = verylonglong \
Longlonglong \
Longlonglongvalue
- The value cannot be written. It is considered as a null string.
# The following keys are associated with null strings.
Key =
Key
- Unicode escape sequence is required for non-ASCII characters
# Escape "Chinese" to \ u4E2D \ u6587
Key \ u4E2D \ u6587
Reference: http://www.766.com/doc/java/util/Properties.html#load (java. io. Reader)
Java * properties configuration file format
#2: one key-value row.
#3: No problems you have mentioned. You can use the load (InputStream in) method of Properties to automatically load configuration items. Then you can use the get (key) method of Properties to obtain the configuration value.
#4: All rows are placed in one row, which is not acceptable, but will be parsed into key = logo. location. The value is/image/logo/mail. host = webmaster@zlex.orgsite.name = zlex Chinese site, in this case, you still need to resolve yourself. Therefore, a configuration item is usually a line.
How to use java basic properties?
The properties file is a text file in the format
Aaa = 1111
Bbb = 2222
Ccc = 1, 3333
This format
Java has a class of java. util. Properties that specifically processes this file.
Import java. io. File;
Import java. io. FileInputStream;
Import java.net. URI;
Public class ZZZZZ {
Public static void main (String [] args) throws Exception {
Java. util. Properties props = new java. util. Properties ();
FileInputStream FCM = new FileInputStream
(New File (new URI ("file: // d:/a.txt ")));
Props. load (FS );
System. out. println (props );
}
}