如何编写代码,使我不会添加重复的记录,而不是覆盖txt文件中的内容 import java.io.*; 公共类储蓄游戏{ 公共静态void main(字符串[]args){ String user=“John”//重写为String user=String.toString(Game.user) 字符串compTime1=“2”; 字符串compTime2=“0”; 字符串compTime3=“0”; 字符串compLevel=“2”; 字符串tokenCollected=“3”; 字符串flipperCollected=“2”; 字符串firebootsCollected=“4”; 字符串wingbootsCollected=“3”; 字符串keysCollected=“3”; saveGame(用户、compTime1、compTime2、compTime3、compLevel、tokenCollected、flipperCollected、firebootsCollected、wingbootsCollected、KeyCollected); } 公共静态void saveGame(字符串用户、字符串compTime1、字符串compTime2、字符串compTime3、字符串compLevel、字符串tokenCollected、字符串flipperCollected、字符串firebootsCollected、字符串wingbootsCollected、字符串keysCollected){ 尝试 { FileWriter fw=新的FileWriter(“Game.txt”,true); BufferedWriter bw=新的BufferedWriter(fw); PrintWriter pw=新的PrintWriter(bw); 打印(“\n”+用户+”、“+compTime1+”、“+compTime2+”、“+compTime3+”、“+completvel+”、“+tokenCollected+”、“+flipperCollected+”、“+firebootsCollected+”、“+wingbootsCollected+”、“+keysCollected”); pw.flush(); 关闭(); }捕获(例外e){ e、 printStackTrace(); System.out.println(“记录未保存”); } } }

如何编写代码,使我不会添加重复的记录,而不是覆盖txt文件中的内容 import java.io.*; 公共类储蓄游戏{ 公共静态void main(字符串[]args){ String user=“John”//重写为String user=String.toString(Game.user) 字符串compTime1=“2”; 字符串compTime2=“0”; 字符串compTime3=“0”; 字符串compLevel=“2”; 字符串tokenCollected=“3”; 字符串flipperCollected=“2”; 字符串firebootsCollected=“4”; 字符串wingbootsCollected=“3”; 字符串keysCollected=“3”; saveGame(用户、compTime1、compTime2、compTime3、compLevel、tokenCollected、flipperCollected、firebootsCollected、wingbootsCollected、KeyCollected); } 公共静态void saveGame(字符串用户、字符串compTime1、字符串compTime2、字符串compTime3、字符串compLevel、字符串tokenCollected、字符串flipperCollected、字符串firebootsCollected、字符串wingbootsCollected、字符串keysCollected){ 尝试 { FileWriter fw=新的FileWriter(“Game.txt”,true); BufferedWriter bw=新的BufferedWriter(fw); PrintWriter pw=新的PrintWriter(bw); 打印(“\n”+用户+”、“+compTime1+”、“+compTime2+”、“+compTime3+”、“+completvel+”、“+tokenCollected+”、“+flipperCollected+”、“+firebootsCollected+”、“+wingbootsCollected+”、“+keysCollected”); pw.flush(); 关闭(); }捕获(例外e){ e、 printStackTrace(); System.out.println(“记录未保存”); } } },java,Java,我应该写些什么,以便在添加名为John的数据时(例如,game.txt文件中已经存在的数据)不会再次添加,而会在game.txt文件中覆盖包含John的记录。我发现只有添加有效。如果文件不大,可以将整个文件读取到内存中。将读取的字符串数据解析为对象列表或对象映射。然后应用新的更改并将整个块重新写入文件。您可以将对象的映射或列表传递给写入函数。为了知道文件中是否已经存在用户“John”的记录,您需要先读取文件,然后再尝试写入新文件 一种方法是: 读文件 检查它是否包含“John”/用户的记录 如果

我应该写些什么,以便在添加名为John的数据时(例如,
game.txt
文件中已经存在的数据)不会再次添加,而会在
game.txt
文件中覆盖包含John的记录。我发现只有添加有效。

如果文件不大,可以将整个文件读取到内存中。将读取的字符串数据解析为对象列表或对象映射。然后应用新的更改并将整个块重新写入文件。您可以将对象的映射或列表传递给写入函数。

为了知道文件中是否已经存在用户“John”的记录,您需要先读取文件,然后再尝试写入新文件

一种方法是:

  • 读文件
  • 检查它是否包含“John”/用户的记录
  • 如果不存在记录,则写入该文件

  • 此逻辑回答了您的问题,但您可能希望将内存中的游戏数据与game.txt文件合并,而不是在存在已保存游戏的情况下避免写入该文件。否则,您的用户在第一次保存游戏后将无法保存任何游戏。

    您可以按如下方式执行:

    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");
            }
        }
    }
    

    你试图覆盖“John”而不是整个文件???您正在尝试向文件中添加其他用户吗?我不清楚你在问什么。试着重新措辞你的问题。如果记录存在,我想重写这行。你的文件应该有多个用户保存的游戏吗?您当前的代码阻止了这一点。是的,我的文件可以有多个用户,只是如果用户具有相同的名称,它将在txt记录中覆盖确切的名称。如果没有,它将添加新用户,如果用户名不在记录中。听起来您真正想要的是合并而不是覆盖。您应该将文件读入内存,找到并覆盖以“John”(或任何用户名)开头的行(如果存在)。如果没有,请将John的游戏数据附加到字符串的末尾,并将其写入磁盘。请注意,CSV不是在一个文件中存储多条记录的唯一方式,也可能不是最好的游戏数据存储方式。非常感谢,但是我不想100%被欺骗这是我修改过的代码,仍然不知道为什么它无法运行。
    John,4,0,0,2,3,2,4,3,5
    Andy,2,0,0,2,3,2,4,3,3