Hey FretDancer69,
I have written this example for you. It will write the scores to a file and then read the file and print the scores.
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class FretDancer69 {
/**
* JavaProgrammingForums.com
*/
public static File file = new File("file.bin");
public static int win, lose, tie;
public static int winFile, loseFile, tieFile;
public static void writeFile() {
String newLine = System.getProperty("line.separator");
try {
Writer output = null;
output = new BufferedWriter(new FileWriter(file));
output.write(Integer.toString(win));
output.write(newLine);
output.write(Integer.toString(lose));
output.write(newLine);
output.write(Integer.toString(tie));
output.close();
System.out.println("File written");
readFile();
} catch (Exception e) {
System.out.println("Error writing file");
e.printStackTrace();
}
}
public static void readFile() {
System.out.println("Reading file");
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
winFile = sc.nextInt();
loseFile = sc.nextInt();
tieFile = sc.nextInt();
}
sc.close();
System.out.println("Read file");
} catch (FileNotFoundException e) {
System.out.println(file + " cannot be found!");
}
}
public static void main(String[] args) throws Exception {
win = 10;
lose = 2;
tie = 1;
FretDancer69 fd = new FretDancer69();
fd.writeFile();
System.out.println();
System.out.println("Results from file: ");
System.out.println("Wins = " + winFile);
System.out.println("Loses = " + loseFile);
System.out.println("Ties = " + tieFile);
}
}
Example output:
File written
Reading file
Read file
Results from file:
Wins = 10
Loses = 2
Ties = 1