Guest User

Untitled

a guest
Oct 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. package com.morenoamor.tutorials;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.Properties;
  8.  
  9. /**
  10. * Properties file reading and writing example
  11. *
  12. * @author Jesús Moreno Amor <jesus@morenoamor.com>
  13. */
  14. public class PropertiesFile
  15. {
  16. public static void main(String[] args)
  17. {
  18. String properties_file_path = "/path/to/file/properties_file.properties";
  19.  
  20. FileInputStream input_stream = null;
  21. FileOutputStream output_stream = null;
  22.  
  23. try
  24. {
  25. Properties properties = new Properties();
  26.  
  27. // Loading from INI format
  28. input_stream = new FileInputStream(properties_file_path);
  29. properties.load(input_stream);
  30. input_stream.close();
  31.  
  32. // Loading from XML fromat
  33. input_stream = new FileInputStream(properties_file_path);
  34. properties.loadFromXML(input_stream);
  35. input_stream.close();
  36.  
  37. // Gettng value of a property
  38. System.out.println("The value of key_name key is: " + properties.getProperty("key_name"));
  39.  
  40. // Setting value of a property
  41. properties.setProperty("keyA", "value1");
  42. properties.setProperty("keyB", "value2");
  43. properties.setProperty("keyC", "value3");
  44.  
  45. // Storing to INI format
  46. output_stream = new FileOutputStream(properties_file_path);
  47. properties.store(output_stream, "Comments");
  48. output_stream.close();
  49.  
  50. // Storing to XML format
  51. output_stream = new FileOutputStream(properties_file_path);
  52. properties.storeToXML(output_stream, "Comments");
  53. output_stream.close();
  54.  
  55. } catch (FileNotFoundException ex) {
  56. ex.printStackTrace();
  57. } catch (IOException ex) {
  58. ex.printStackTrace();
  59. }
  60. }
  61. }
Add Comment
Please, Sign In to add comment