Reading and writing Properties files in Java

In java properties File is a configuration file, which is mainly used to express configuration information. The file type is * Properties is a text file. The file content is in the format of "key = value". In the properties file, you can use "\" as a comment. The properties file is used in many places in Java programming and is easy to operate.

1, properties file

The Properties class exists in the package java Util, which inherits from Hashtable. It provides several important methods:

1.getProperty(String key), read the search property in this property list with the specified key. That is, get the value corresponding to the key through the parameter key.

2.load (InputStream inputStream) to read the attribute list (key and element pairs) from the input stream. Get all key value pairs in the specified file by loading it. Search by providing getProperty(String key)

3.setProperty (String key,String value), call the Hashtable method put. It sets key value pairs by calling the put method of the base class

4.store(OutputStream outputStream,String comments), in a format suitable for loading into the Properties table using the load method, writes the property list (key and element pairs) in the Properties table to the output stream. In contrast to the load method, this method writes key value pairs to the specified file.

5.clear(), clear all the key value pairs. This method is provided in the base class.

2, Java methods for manipulating Properties files

The java properties file needs to be placed under the classpath so that the program can read it. The classpath is actually the storage path of the java class or library. In the java project, the properties are placed in the class file. In a web application, the simplest way is to put it in the WEB-INF\classes directory of the web application, or in other files. In this case, when setting the classpath environment variable, you need to add this folder path to the classpath variable, so that you can read it.

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Test04 {
	public static void main(String[] args) {
		//Read the files under the relative path through class path
		//class path: the compiled bin directory of the current project
		
		//data. The properties file is saved in the class path root directory
		//InputStream in = Test04.class.getResourceAsStream("/data.properties")
		
		//Temp The properties file is saved in the /Test directory under the class path root directory	
		try (InputStream in = Test04.class.getResourceAsStream("/Test/temp.properties")) {
			
			//Load read
			Properties props = new Properties();
			props.load(in);
			
			System.out.println(props);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}					
	}
}


3, Read and write operations of Properties file

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class test03 {
	public static void main(String[] args) {
		// Reading Properties format file
		// Create input stream
		try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\data\\data.properties"))) {
			Properties props = new Properties();

			props.load(bis);// Load the input stream into the Properties collection object

			// Get value according to key
			System.out.println(props.get("cn"));
			System.out.println(props.get("kr"));
		} catch (IOException e) {

			e.printStackTrace();
		}

		// Writing Properties format file
		Properties props = new Properties();
		props.put("F1", "2344");
		props.put("F2", "5423");
		props.put("F3", "8782");
		props.put("F4", "4324");
		props.put("F5", "7542");

		// Use the output stream to write KV key value pairs in the properties collection to * Properties file
		try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\data\\demo.properties"))) {
			props.store(bos, "just do IT");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


 

Tags: Java Eclipse

Posted by ngoweb on Wed, 01 Jun 2022 20:17:11 +0530