import java.io.*;import java.util.Properties;/** * Created by James on 12/2/2015. */public class PropertiesDemo{ public static void main(String[] args) { /* Properties util is a great way for storing configuration data. it works with a file (though not yaml) it also makes reading and writing configurations very easy. and then getting a specific value from a configuration is also easy this is how i would do configuration data */ try { Properties properties = new Properties(); File propFile = new File("./configuration.properties"); if (propFile.exists()) { FileInputStream fileStream = new FileInputStream(propFile); properties.load(fileStream); System.out.println(properties.getProperty("TEXT")); } else { properties.setProperty("TEXT", "This is a properties demo"); propFile.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(propFile); properties.store(fileOutputStream, "System Configuration"); fileOutputStream.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } }}
that is for using the Properties stuff
import java.io.*;/** * Created by James on 12/2/2015. */public class PrintWriterDemo{ public static void main(String[] args) { //do stuff in here try { /* PrintWriters are great for making writing to some type of output (console, network, file) more like writing to the console. you get a println method as well as print. but can also simply utilize write */ PrintWriter writer = new PrintWriter(new FileWriter(new File("./foo.txt"))); writer.println("this is a demo with a print writer"); writer.close(); } catch(IOException ioe) { ioe.printStackTrace(); } }}
PrintWriter demo, similar to BufferedWriter but little less explicit for when data is flushed
import java.io.*;/** * Created by James on 12/2/2015. */public class BufferedWriterDemo{ public static void main(String[] args) { //do stuff in here try { /* BufferedWriters are great for maintaining control over the output of the file and when data is actually written (either to the network or to the disk) */ BufferedWriter writer = new BufferedWriter(new FileWriter(new File("./foo.txt"))); writer.write("this is a line"); writer.flush(); writer.close(); } catch(IOException ioe) { ioe.printStackTrace(); } }}
BufferedWriter demo. these are useful for when you want to explicity control when the data is written to the file. but i would say go for PrintWriter or the Properties utility.