Java operation of Properties file details and simple examples
- 2020-06-07 04:32:19
- OfStack
Java operations on Properties files
Introduction to the
We often use in Java java. util. Properties. Properties class to parse the Properties file, Properties is Java commonly used configuration file format file, it is used to store key-value pairs in the file, the keys and values with equal space, format is as follows:
name=shawearn
Properties class is java. util. Hashtable < Object, Object > For mapping between keys and values.
In working with Properties format files, we often use the following method of the Properties class:
Properties() : Used to create 1 object with no attribute value Properties;
void load(InputStream inStream) : Loads the property list from the input stream; void store(OutputStream out, String comments) : Saves the property list to a file according to the output stream; String getProperty(String key) : Gets the value of the specified key; void setProperty(String key, String value) : Set the value of the specified key. If the specified key already exists in the original attribute value list, it will be overwritten. If the specified key does not exist in the original property value list, new;Write to Properties file:
// create 1 a Properties Instance;
Properties p = new Properties();
// for Properties Set properties and property values;
p.setProperty("name", "shawearn");
p.setProperty("address", "XX province XX The city ");
// save Properties to shawearn.properties File;
FileOutputStream out = new FileOutputStream("shawearn.properties");
p.store(out, "Create by Shawearn!");
out.close();
Read the Properties file:
// create 1 a Properties Instance;
Properties p = new Properties();
// Read the configuration file;
FileInputStream in = new FileInputStream("shawearn.properties");
// Load the configuration file to Properties Instance;
p.load(in);
in.close();
Finally, the test code is attached:
package com.shawearn.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/**
* @author Shawearn
*
*/
public class TestProperties {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
TestProperties t = new TestProperties();
// Test write;
t.testWrite();
// Test read;
t.testRead();
}
/*
* The test for Properties Write the file;
*/
private void testWrite() throws IOException {
// create 1 a Properties Instance;
Properties p = new Properties();
// for Properties Set properties and property values;
p.setProperty("name", "shawearn");
p.setProperty("address", "XX province XX The city ");
// save Properties to shawearn.properties File;
FileOutputStream out = new FileOutputStream("shawearn.properties");
p.store(out, "Create by Shawearn!");
out.close();
System.out.println(" Write successful! ");
}
/*
* The test for Properties The reading operation of files;
*/
private void testRead() throws IOException {
// create 1 a Properties Instance;
Properties p = new Properties();
// Read the configuration file;
FileInputStream in = new FileInputStream("shawearn.properties");
// Load the configuration file to Properties Instance;
p.load(in);
in.close();
// To obtain Properties Everything in the file key ;
Set<String> keys = p.stringPropertyNames();
// I'm going through all of them key ;
for (String key : keys) {
// To obtain Properties In the file key The corresponding value ;
Object value = p.get(key);
// The input key And the corresponding value ;
System.out.println(key + " => " + value);
}
}
}
Console output results:
address => XX province XX The city
name => shawearn
shawearn.properties
#Create by Shawearn!
#Thu Nov 19 12:43:41 CST 2015
name=shawearn
address=XX \u7701 XX \u5E02
Thank you for reading, I hope to help you, thank you for your support to this site!