Reputation: 49
import java.io.*;
public class SaveGame {
public static void main(String[] args) {
String user = "John";//rewrite as String user = String.toString(Game.user)
String compTime1 = "2";
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
{
FileWriter fw = new FileWriter("Game.txt",true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
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");
}
}
}
What should I write so that when I add the data with name John for example, which already exists in the game.txt
file will not add again whereas it will overwrite the record with John in it in the game.txt
. I found only the adding works.
Upvotes: 0
Views: 74
Reputation: 79435
You can do it as follows:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class SaveGame {
public static void main(String[] args) {
saveGame("John", "2", "0", "0", "2", "3","2","4","3","3");
saveGame("Andy", "2", "0", "0", "2", "3","2","4","3","3");
saveGame("John", "4", "0", "0", "2", "3","2","4","3","5");
}
public static void saveGame(String user, String compTime1, String compTime2, String compTime3, String compLevel,
String tokenCollected, String flipperCollected, String firebootsCollected, String wingbootsCollected,
String keysCollected) {
String strToBeSaved = user + "," + compTime1 + "," + compTime2 + "," + compTime3 + "," + compLevel + ","
+ tokenCollected + "," + flipperCollected + "," + firebootsCollected + "," + wingbootsCollected + ","
+ keysCollected;
List<String> list = new ArrayList<String>();
String line;
File file = new File("Game.txt");
boolean userAlreadyExists = false;
BufferedReader br;
if (file.exists()) {
try {
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
list.add(line); // Add each line to the list
}
br.close();
} catch (Exception e) {
e.printStackTrace();
//System.out.println("File could not be read for processing");
}
for (int i = 0; i < list.size(); i++) {
line = list.get(i);
if (line != null && user != null && line.toLowerCase().contains(user.toLowerCase())) {
userAlreadyExists = true;
list.set(i, strToBeSaved); // Replace the existing user data with the new data
break;
}
}
}
FileWriter fw;
try {
if (userAlreadyExists)
fw = new FileWriter(file); // Overwrite if the file already exists
else
fw = new FileWriter(file, true); // Open the file in append mode
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
if (userAlreadyExists) {
for (String ln : list) {
pw.println(ln); // Rewrite all the lines in the list
}
} else {
pw.println(strToBeSaved);
}
pw.flush();
pw.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Record not saved");
}
}
}
I have put the important comments in code so that you can understand it easily. Feel free to let me know in case you have any doubt.
The content of the file after executing the program:
John,4,0,0,2,3,2,4,3,5
Andy,2,0,0,2,3,2,4,3,3
Upvotes: 0
Reputation: 1265
In order to know whether a record for user "John" already exists in the file, you will need to read the file before attempting to write out a new file.
One approach is to:
This logic answers your question, but you probably want to merge the game data in memory with the Game.txt file rather than avoid writing the file if an existing saved game exists. Otherwise your user will never be able to save any games after his first.
Upvotes: 0
Reputation: 84
If the file is not large you can read entire file to memory.Parse read string data to list or map of objects. Then apply new changes and re-write entire block again to file. You can pass the map or list of your objects to the writing function.
Upvotes: 1