Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.morenoamor.tutorials;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Properties;
- /**
- * Properties file reading and writing example
- *
- * @author Jesús Moreno Amor <jesus@morenoamor.com>
- */
- public class PropertiesFile
- {
- public static void main(String[] args)
- {
- String properties_file_path = "/path/to/file/properties_file.properties";
- FileInputStream input_stream = null;
- FileOutputStream output_stream = null;
- try
- {
- Properties properties = new Properties();
- // Loading from INI format
- input_stream = new FileInputStream(properties_file_path);
- properties.load(input_stream);
- input_stream.close();
- // Loading from XML fromat
- input_stream = new FileInputStream(properties_file_path);
- properties.loadFromXML(input_stream);
- input_stream.close();
- // Gettng value of a property
- System.out.println("The value of key_name key is: " + properties.getProperty("key_name"));
- // Setting value of a property
- properties.setProperty("keyA", "value1");
- properties.setProperty("keyB", "value2");
- properties.setProperty("keyC", "value3");
- // Storing to INI format
- output_stream = new FileOutputStream(properties_file_path);
- properties.store(output_stream, "Comments");
- output_stream.close();
- // Storing to XML format
- output_stream = new FileOutputStream(properties_file_path);
- properties.storeToXML(output_stream, "Comments");
- output_stream.close();
- } catch (FileNotFoundException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment