I'm trying to move a file to a different directory using .renameTo()

This isn't working, as the file is still in the original dir after the code has completed, and there is a copy of the file in the target directory.

I tried deleting the original file, which doesn't work.

The section of code that does the move and delete is in a thread, of which there can be multiple instances running, so I only move and delete the file (or try to, at least) after I locked the file.
Would this cause a problem?

I've included the code that locks, moves and deletes the file.

public static String ModifyOverrideFile() {
        // Concatenate OverrideFolder with OverrideFile, (Also, generate the actual overrive file)and use it as OverrideFilePath - return OverrideFilePath
        File file = new File(fileName);
        Logger logger = new Logger();
        RandomAccessFile raf = null;
        FileLock lock = null;
        String overrideFilePath = "";
        try {
            // Create a temporary retain folder
            File tempDir = new File(ThreadStarter.overrideFolder + "tempRetain");

            if (!tempDir.exists()) {
                boolean success = (tempDir.mkdir());
            }

            // Try to lock the file so it can be moved
            try {
                raf = new RandomAccessFile(file, "rw");
                FileChannel channel = raf.getChannel();

                lock = channel.tryLock();

                if (lock != null) {
                    // File is locked
                     accessLock(file);
                }
            } finally {
                if (lock != null) {
                    lock.release();
                }
            }

            // Move file to new directory
            String newFile = tempDir.getAbsolutePath() + "\\" + file.getName();

            overrideFilePath = ThreadStarter.overrideFolder + file.getName() + ".xml";
            boolean success = file.renameTo(new File(tempDir, file.getName()));
            if (!success) {
                // File was not successfully moved
            } else {
                boolean deleteSuccess = file.delete();
                if (!deleteSuccess) {
                    System.out.println("COULD NOT DELETE FILE");
                }
            }

            String contents = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                    "<JobDatasourceOverride>\n" +
                    "<JobStepDatasourceOverride jobStepID=\"0\" jobStepName=\"1\" defineAssembleDatasource=\"\" definedTriggerDatasource=\"\" overridenAssembleDatasource=\"\" overridenAssembleDataFile=\"" + newFile + "\" overridenTriggerDatasource=\"\" overridenTriggerDataFile=\"\" />\n" +
                    "</JobDatasourceOverride>";

                BufferedWriter out = new BufferedWriter(new FileWriter(overrideFilePath));
                out.write(contents);
                out.close();
                logger.log("Created override file " + newFile + ".xml");

            
        } catch (FileNotFoundException fnf) {
        } catch (IOException io) {
            System.out.println("***************************");
            io.printStackTrace();
        }
        return overrideFilePath;
    }

    static void accessLock(File file) {
        try {
            FileInputStream input = new FileInputStream(file);
            int data = input.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Any help would be greatly appreciated, as I have been struggling with problem since yesterday, and haven't progressed at all.

Thanks in advance.

Okay, it turns out the file doesn't get successfully removed.

I don't know why not. Does anyone have an idea?

There is a lot more logic in your code than is needed for testing. The following code worked for me. It moved the file from one directory to the other:

public class MovingFile {

  public static void main(String[] args) {
      File fromFile = new File("FromFolder\\MoveMe.txt");
      File toFile = new File("ToFolder\\MoveMe.txt");
      System.out.println("renameTo = " + fromFile.renameTo(toFile));  // renameTo = true
  }

Thanks a lot, this worked for me as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.