Showing posts with label properties. Show all posts
Showing posts with label properties. Show all posts

Monday, April 30, 2018

Read properties from an xml file !!

Using Properties class:
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.util.Enumeration;
  4. import java.util.Properties;

  5. public class ReadPropertiesXmlFile {
  6. public static void main(String[] args) throws Exception {
  7. File file = new File("D:\\prop.xml");
  8. FileInputStream fileInput = new FileInputStream(file);
  9. Properties properties = new Properties();
  10. properties.loadFromXML(fileInput);
  11. fileInput.close();

  12. Enumeration enuKeys = properties.keys();
  13. while (enuKeys.hasMoreElements()) {
  14. String key = (String) enuKeys.nextElement();
  15. String value = properties.getProperty(key);
  16. System.out.println(key + ": " + value);
  17. }
  18. }
  19. }
Output:
favoriteSeason: spring
favoriteDay: today
favoriteFruit: apple

prop.xml - file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Here are some favorites</comment>
<entry key="favoriteSeason">spring</entry>
<entry key="favoriteFruit">apple</entry>
<entry key="favoriteDay">today</entry>

</properties>

Read the data from properties file !!

Using Properties class:
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.util.Enumeration;
  5. import java.util.Properties;

  6. public class ReadPropertiesFiles {
  7. public static void main(String[] args) throws IOException {

  8. File file = new File("D:\\contacts.properties");
  9. FileInputStream fis = new FileInputStream(file);
  10. Properties prop = new Properties();
  11. prop.load(fis);
  12. fis.close();

  13. Enumeration enuKeys = prop.keys();
  14. while (enuKeys.hasMoreElements()) {
  15. String key = (String) enuKeys.nextElement();
  16. String value = prop.getProperty(key);
  17. System.out.println(key + " = " + value);
  18. }
  19. }
  20. }
Output:
delhi = sumit
japan = chet
bangalore = raju
chennai = ishaan

Sunday, April 29, 2018

Write data from properties to xml file !!

Using Properties class:
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.util.Properties;

  4. public class WritePropertiesXmlFile {
  5. public static void main(String[] args) throws Exception {
  6. Properties properties = new Properties();
  7. properties.setProperty("suresh", "2000");
  8. properties.setProperty("mahesh", "1200");
  9. properties.setProperty("ritesh", "2100");

  10. File file = new File("D:\\test2.xml");
  11. FileOutputStream fileOut = new FileOutputStream(file);
  12. properties.storeToXML(fileOut, "balance-sheet");
  13. fileOut.close();
  14. }
  15. }
Output:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>balance-sheet</comment>
<entry key="ritesh">2100</entry>
<entry key="suresh">2000</entry>
<entry key="ishaan">0000</entry>
<entry key="mahesh">1200</entry>
</properties>

Write the data in properties file !!

Using Properties class:
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.util.Properties;

  5. public class WritePropertiesFiles {

  6. public static void main(String[] args) throws IOException {
  7. Properties prop = new Properties();
  8. prop.setProperty("bangalore", "raju");
  9. prop.setProperty("delhi", "ritesh");
  10. prop.setProperty("chennai", "ishaan");
  11. prop.setProperty("japan", "chet");

  12. File file = new File("D:\\contacts.properties");
  13. FileOutputStream fileStream = new FileOutputStream(file);
  14. prop.store(fileStream, "store the contacts");
  15. fileStream.close();
  16. }
  17. }
Output:
#store the contacts
#Sun Apr 29 20:22:15 IST 2018
delhi=ritesh
japan=chet
chennai=ishaan
bangalore=raju

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...