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
            } finally {
                if (lock != null) {

            // 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" +

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

        } catch (FileNotFoundException fnf) {
        } catch (IOException io) {
        return overrideFilePath;

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

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

Thanks in advance.

6 Years
Discussion Span
Last Post by P00dle

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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.