I have been trying to rename a text file in java using the following code.
tempFile.renameTo(inFile)
However, it does not seem to be renaming the file.
The aim of my project is to edit a line of text in a text file.
1. First reads the file and stores the line as oldLine.
2. Takes the edited line and stores it as newLine.
3. Writes the newLine to a temporary file.
4. Deletes the main file and renames the temporary file to the main file's name.
Is there an alternative to this rename function?
Edit with Full Code
   try {
        File inFile = new File("Members.txt");
        File tempFile = new File("MembersTemp.txt");
        BufferedReader BR = new BufferedReader(new FileReader(inFile));
        PrintWriter PW = new PrintWriter(new FileWriter(tempFile));
        inFile.setReadable(true);
        inFile.setWritable(true);
        tempFile.setReadable(true);
        tempFile.setWritable(true);
        String line = null;
        while ((line = BR.readLine()) != null) {
            if (!old.equals(newLine)) {
                //String replace = old.replace(old, newLine);
                PW.println(newLine);
                PW.flush();
            }
        }
        PW.close();
        BR.close();
        if (!tempFile.renameTo(inFile)) {
            JOptionPane.showMessageDialog(
                null,
                "Error",
                "Edit File",
                JOptionPane.WARNING_MESSAGE);
        } else {
              JOptionPane.showMessageDialog(
              null,
              "File saved successfully",
              "Edit File",
              JOptionPane.INFORMATION_MESSAGE);
        }
        dispose();
        new Edit_Member().setVisible(true);
    } catch (IOException | HeadlessException e) {
        e.printStackTrace(); 
        JOptionPane.showMessageDialog(
        null,
        "Catch Error",
        "Edit File",
        JOptionPane.WARNING_MESSAGE);
    }
Updated with Files.move
Path source = tempFile.toPath();
Path newdir = inFile.toPath();
Files.move(source, newdir, REPLACE_EXISTING);
The above code gives me the following error:
java.nio.file.FileSystemException: Members.txt: The process cannot access the file because it is being used by another process.
I thought I had closed all my readers and writers, so this error really shouldn't be present.