Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class SaveGame {
- private static Scanner x;
- public static void main(String[] args) {
- File newfile = new File("temp.txt");
- if(!newfile.exists()) {
- try {
- newfile.createNewFile();
- }
- catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- String user = "June";//rewrite as String user = String.toString(Game.getUser())
- String compTime1 = "7";//rewrite as String compTime1 = String.toString(Game.getCompTime1())
- String compTime2 = "0";
- String compTime3 = "0";
- String compLevel = "2";
- String tokenCollected = "3";
- String flipperCollected = "2";
- String firebootsCollected = "4";
- String wingbootsCollected = "3";
- String keysCollected = "3";
- saveGame(user,compTime1,compTime2,compTime3,compLevel, tokenCollected, flipperCollected,firebootsCollected,wingbootsCollected,keysCollected);
- }
- public static void saveGame(String user, String compTime1, String compTime2, String compTime3, String compLevel, String tokenCollected, String flipperCollected, String firebootsCollected, String wingbootsCollected, String keysCollected) {
- try
- {
- String olduser = "";//rewrite as String user = String.toString(Game.user)
- String oldcompTime1 = "";
- String oldcompTime2 = "";
- String oldcompTime3 = "";
- String oldcompLevel = "";
- String oldtokenCollected = "";
- String oldflipperCollected = "";
- String oldfirebootsCollected = "";
- String oldwingbootsCollected = "";
- String oldkeysCollected = "";
- String tempFile = "temp.txt";
- File oldFile = new File("Game.txt");
- File newFile = new File("temp.txt");
- FileWriter fw = new FileWriter(oldFile,true);
- BufferedWriter bw = new BufferedWriter(fw);
- PrintWriter pw = new PrintWriter(bw);
- FileWriter fw1 = new FileWriter(newFile,true);
- BufferedWriter bw1 = new BufferedWriter(fw1);
- PrintWriter pw1 = new PrintWriter(bw1);
- x = new Scanner(new File("Game.txt"));
- x.useDelimiter("[,\n]");
- boolean yes = true;
- while(x.hasNext())
- {
- olduser = x.next();
- oldcompTime1 = x.next();
- oldcompTime2 = x.next();
- oldcompTime3 = x.next();
- oldcompLevel = x.next();
- oldtokenCollected = x.next();
- oldflipperCollected = x.next();
- oldfirebootsCollected = x.next();
- oldwingbootsCollected = x.next();
- oldkeysCollected = x.next();
- if(!olduser.contentEquals(user)){
- pw1.print("\n" +user + "," + compTime1 + "," + compTime2 + "," + compTime3 + "," + compLevel + "," + tokenCollected + "," + flipperCollected + "," + firebootsCollected + "," + wingbootsCollected + "," + keysCollected);
- yes = false;
- }
- }
- x.close();
- pw1.flush();
- pw1.close();
- oldFile.delete();
- File dump = new File("Game.txt");
- newFile.renameTo(dump);
- if(yes) {
- pw.print("\n" +user + "," + compTime1 + "," + compTime2 + "," + compTime3 + "," + compLevel + "," + tokenCollected + "," + flipperCollected + "," + firebootsCollected + "," + wingbootsCollected + "," + keysCollected);
- pw.flush();
- pw.close();
- }
- }catch(Exception e) {
- e.printStackTrace();
- System.out.println("Record not saved");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement