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

Jussi Pohjolainen

April 26, 2020
Tweet

More Decks by Jussi Pohjolainen

Other Decks in Technology

Transcript

  1. 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); } }
  2. 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")); } }
  3. 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); } }
  4. 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>
  5. 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")); } }
  6. Jackson • Java classes to json mapping • External class

    library, add dependency • You can use Jackson to save for example preferences file
  7. Pojo public class Dog { private String name; public void

    setName(String name) { this.name = name; } public String getName() { return this.name; } }
  8. 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()); } }