-2

Hi I am having trouble deleting and renaming my file this is because my file is still open but when I try to close my file I then get a stream closed which means my file is closed, my question how do I get my code to delete and rename my file even though my file stream is closed, all my txt files are in the correct folder

public static void updateRecord(int recordID, int recordColumnIndex, String newValue,String fileName) throws IOException {
        java.io.PrintWriter writer;
        try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(fileName))) {
            writer = new java.io.PrintWriter("temp.txt");
            String line;
            // Read file line by line...
            while ((line = reader.readLine()) != null) {
                if (line.trim().isEmpty()) {
                    writer.println(line);
                    continue;
                }
                // Split each read line to separate data.
                String[] lineParts = line.split("\\s*,\\s*");

            /* If the record ID value matches the supplied ID value
               then make the desired changes to that line.    */
                if (Integer.parseInt(lineParts[0]) == recordID) {
                    lineParts[recordColumnIndex] = newValue;
                    // Rebuild the data line...
                    StringBuilder sb = new StringBuilder(String.valueOf(recordID));
                    for (int i = 1; i < lineParts.length; i++) {
                        sb.append(",").append(lineParts[i]);
                    }
                    line = sb.toString();
                }
                writer.println(line);  // write line to `temp.tmp` file.
                writer.flush();        // Immediate write.
                reader.close();
            }
        }
        writer.close();

        File inputFile = new File("tasks.txt");
        File outFile = new File("temp.txt");

        if (inputFile.exists()) {
            Files.delete(Path.of("tasks.txt"));
            outFile.renameTo(inputFile);
            System.out.println("test");
        }else{
            System.out.println("error");
        }

    }
chris
  • 13
  • 3
  • 1
    Check that you haven't opened same file in other code. What is the actual exception and stack trace? – DuncG Oct 07 '22 at 09:44

1 Answers1

0

Your problem most likely is caused elsewhere - check all file I/O operations. I suggest you tidy your code so that the file handling is clearer, consistently using NIO Path and Files, not repeating filename definitions, and that all streams are closed using try-with-resources handling:

public static void updateRecord(int recordID, int recordColumnIndex, String newValue, Path fileName) throws IOException {

    Path tempOutFile = Path.of("temp.txt");
    Path mainInputFile = Path.of("tasks.txt");

    try (BufferedReader reader = Files.newBufferedReader(fileName);
         PrintWriter writer = new PrintWriter(Files.newBufferedWriter(tempOutFile))) {

        ...
    }
    // Replace main file:
    Files.deleteIfExists(mainInputFile);
    Files.move(tempOutFile, mainInputFile);
}
DuncG
  • 12,137
  • 2
  • 21
  • 33