0

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.

JSmithers
  • 83
  • 8
  • 1
    From your description, it is very likely due to the fact that you didn't close properly your InputStream. But there's no code so... please make a [mcve]. – Tunaki Feb 26 '16 at 13:24
  • In your file read loop, `newLine` is not defined, do you mean `line`? – SamTebbs33 Feb 26 '16 at 13:39
  • The variable newLine is passed to the method. So it does not need to be defined. – JSmithers Feb 26 '16 at 13:43
  • Do you have your test file open in an editor/viewer? Or a previous debugging session that has not yet terminated? The "(an)other process" is usually just that - some process other than the code that you are executing. – Rob Feb 26 '16 at 14:45
  • I have closed Netbeans and reopened it and there doesn't seem to be any processes running in the background. However, the file still doesn't seem to rename. – JSmithers Feb 26 '16 at 14:51

1 Answers1

0

I'm not sure what is going wrong without seeing the code, but since Java SE 7 the recommended way to move/ rename files is Files.move (NIO.2 File API).

Puce
  • 37,247
  • 13
  • 80
  • 152
  • Does this work even if the text files are in the same directory? – JSmithers Feb 26 '16 at 13:29
  • You wrote you're deleting the main file first. Besides there is a REPLACE_EXISTING option. – Puce Feb 26 '16 at 13:30
  • From Javadoc: "Move or rename a file to a target file" – Puce Feb 26 '16 at 13:31
  • 1
    I tried using Files.move but get the following issue: java.nio.file.FileSystemException: Members.txt: The process cannot access the file because it is being used by another process. – JSmithers Feb 26 '16 at 13:50