Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Process API

Process API

Simple examples how to execute shell commands using Java

D9e65f4b0af059ae9ba243c8c2265e4f?s=128

Jussi Pohjolainen

April 26, 2020
Tweet

Transcript

  1. Properties API Jussi Pohjolainen

  2. To store preferences • Simple API to • fetch •

    store • key value pairs
  3. import java.io.*; import java.util.*; class Main { public static void

    main(String [] args) throws Exception { Properties prop = new Properties(); // set key and value prop.setProperty("fontSize", "24"); prop.setProperty("fontColor", "RGB(255,255,255)"); String optionalComments = "My preferences file"; prop.store(new FileWriter("./myfile.prop"), optionalComments); } }
  4. myfile.prop #My preferences file #Sun Apr 26 10:56:55 EEST 2020

    fontSize=24 fontColor=RGB(255,255,255)
  5. Reading import java.io.*; import java.util.*; class Main { public static

    void main(String [] args) throws Exception { Properties prop = new Properties(); prop.load(new FileReader("./myfile.prop")); System.out.println(prop.get("fontSize")); System.out.println(prop.get("fontColor")); } }
  6. Storing XML import java.io.*; import java.util.*; class Main { public

    static void main(String [] args) throws Exception { Properties prop = new Properties(); // set key and value prop.setProperty("fontSize", "24"); prop.setProperty("fontColor", "RGB(255,255,255)"); String optionalComments = "My preferences file"; prop.storeToXML(new FileOutputStream("./myfile.xml"), optionalComments); } }
  7. myfile.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>My

    preferences file</comment> <entry key="fontSize">24</entry> <entry key="fontColor">RGB(255,255,255)</entry> </properties>
  8. Reading import java.io.*; import java.util.*; class Main { public static

    void main(String [] args) throws Exception { Properties prop = new Properties(); prop.loadFromXML(new FileInputStream("./myfile.xml")); System.out.println(prop.getProperty("fontSize")); System.out.println(prop.getProperty("fontColor")); } }
  9. Jackson

  10. Jackson • Java classes to json mapping • External class

    library, add dependency • You can use Jackson to save for example preferences file
  11. Dependency <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.3</version> </dependency> </dependencies>

  12. Pojo public class Dog { private String name; public void

    setName(String name) { this.name = name; } public String getName() { return this.name; } }
  13. Main import com.fasterxml.jackson.databind.ObjectMapper; import java.io.*; class Main { public static

    void main(String [] args) throws Exception { Dog spot = new Dog(); spot.setName("Spot"); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValue(new File("./file.json"), spot); Dog temp = objectMapper.readValue(new File("./file.json"), Dog.class); System.out.println(temp.getName()); } }