Today I'm goin to show you how to write config files in two different languages Java and Python

  • Properties
  • ConfigParser

So lets get down to it

Python

Python uses a libary called configparser

import configparser
import os

def Set():

	config = configparser.RawConfigParser()
	print("Please Enter Twitter Public Key ")
	TPK = input()
	print("Please Enter Twitter Private Key ")
    TPRK = input()
	print("Please note private key for twitter is not stored")
	config.add_section('Twitter')
	config.set('Twitter', 'Public', TPK)
	print("Please wait as your Access token is fetched")
	twitter = Twython(TPK, TPRK, oauth_version=2)
	ACCESS_TOKEN = twitter.obtain_access_token()
	config.set('Twitter','Access',ACCESS_TOKEN)
	with open(GetDir() + "/" + 'config.cfg', 'w') as 			configfile:
    	config.write(configfile)

This is a basic starter from my last post that will get you started in python ignore the part about twitter = twython that is a libary for autheticating against twitter which I may cover in a future post

Java

Java uses a libary called Properties

import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Stateloader {
String score;
Properties prop;
InputStream inputStream = null;

public void Save() {
	Properties prop = new Properties();
	score = "10";
	try {
		prop.setProperty("High Score", score);
		prop.store(new FileOutputStream("c://config.properties"), null);
	} catch (IOException ex) {
		System.err.println("Can't let you do that Dave");
	}

}

public void Load() {
	Properties prop = new Properties();

	try {
		InputStream inputStream = new FileInputStream(
				"c://config.properties");
		prop.load(inputStream);
		String Sscore = prop.getProperty("High Score");
		System.out.println(Sscore);
	} catch (IOException ex) {
		System.err.println("Can't let you do that Dave");
	} finally {
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
				System.err.println("Can't let you do that Dave");
			}
		}
	}

	}
}

The formatting isn't perfect but this is just a proof of concept this basic example will allow you to take the variable score and make it a value in a properties file Save() sets the value to the integer value of score and writes it and Load() loads the value of the property to the files version and puts it in the score varible