Search code examples
javafilewriterbufferedwriter

Writing to File duplicating data the second time JAVA


I'm creating a program to remove doctors from an arrayList that is utilising a queue. This works the first time perfectly however, the second time it's duplicating the data inside the text file. How can I solve this?

/**
 * 
 * @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();
}

Solution

  • This is not 100% solution but I think it will give you the right directions. I don't want to do 100% work for you :)

    In my comment I said

    1. Read file content
    2. Store it in variable
    3. Remove file
    4. Remove doctors from variable
    5. Write variables to new file

    So, to read file content we would use something file this (if it's txt file):

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

    This method returns String as file content. We can store it in a variable like this:

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

    Next step would be to remove our file. Since we have it in memory, and we don't want duplicate values...

    file.delete();

    Now we should remove our doctors from fileContent. It's basic String operations. I would recommend using method replace() or replaceAll().

    And after the String manipulation, just write fileContent to our file again.

    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();