Skip to content

Instantly share code, notes, and snippets.

@justgage
Created May 2, 2014 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justgage/8510a8d1fcc3a68d67e0 to your computer and use it in GitHub Desktop.
Save justgage/8510a8d1fcc3a68d67e0 to your computer and use it in GitHub Desktop.
A simple example of writing a java object to a file.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
class wallet implements java.io.Serializable {
private double balence;
public double check() {
return balence;
}
public double add(double num) {
return balence += num;
}
public static void main(String[] args) {
if (args.length > 0) {
String filename = args[0];
wallet mywallet = new wallet();
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject(mywallet);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Please enter wallet name.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment