【问题标题】:Writing to File duplicating data the second time JAVAJAVA第二次写入文件复制数据
【发布时间】:2017-12-02 09:19:46
【问题描述】:

我正在创建一个程序以从使用队列的 arrayList 中删除医生。然而,这第一次完美地工作,第二次它复制文本文件中的数据。我该如何解决这个问题?

/**
 * 
 * @throws Exception 
 */
public void writeArrayListToFile() throws Exception {

    String path = "src/assignment1com327ccab/DoctorRecordsFile.txt";
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(path));
    BufferedWriter br = new BufferedWriter(os);
    PrintWriter out = new PrintWriter(br);
    DoctorNode temp; //create a temporary doctorNode object
    temp = end; //temp is equal to the end of the queue
    //try this while temp is not equal to null (queue is not empty)
    StringBuilder doctor = new StringBuilder();

    while (temp != null) {
        {

            doctor.append(temp.toStringFile());
            doctor.append("\n");
            //temp is equal to temp.getNext doctor to get the next doctor to count
            temp = temp.getNext();
        }


    }
    System.out.println("Finished list");
    System.out.println("Doctors is : " + doctor.toString());
    out.println(doctor.toString());
    System.out.println("Done");

    br.newLine();
    br.close();
}

【问题讨论】:

  • 读取文件内容;将其存储在变量中;删除文件;从变量中删除医生;将变量写入新文件;
  • 你好 Rafal,我该怎么做?道歉只需要一只手
  • 看看我的回答

标签: java filewriter bufferedwriter


【解决方案1】:

这不是 100% 的解决方案,但我认为它会为您提供正确的方向。我不想为你做 100% 的工作:)

我在评论中说

  1. 读取文件内容
  2. 将其存储在变量中
  3. 删除文件
  4. 从变量中删除医生
  5. 将变量写入新文件

因此,要读取文件内容,我们将使用文件 this(如果是 txt 文件):

public static String read(File file) throws FileNotFoundException {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file.getAbsoluteFile()));

            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                line = br.readLine();
                if (line != null) sb.append(System.lineSeparator());

            }
            String everything = sb.toString();
            return everything;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

此方法返回String 作为文件内容。我们可以将它存储在这样的变量中:

String fileContent = MyClass.read(new File("path to file"));

下一步是删除我们的文件。因为我们在内存中有它,而且我们不想要重复的值......

file.delete();

现在我们应该从fileContent 中删除我们的医生。这是基本的String 操作。我建议使用方法replace()replaceAll()

String 操作之后,只需将fileContent 再次写入我们的文件即可。

File file = new File("the same path");
file.createNewFile();
Writer out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file, true), "UTF-8"));
out.write(fileContent);
out.flush();
out.close();

【讨论】:

  • @HelpImInTrouble 很高兴我能帮上忙 :) 请接受我的回答 :)
  • 另外,我应该把 file.delete() 放在哪里?
  • @HelpImInTrouble 在将文件内容保存到变量之后但在创建新文件之前。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多