1

This is a simple class to show 4 cases. Sorry for my english (is bad)

public static void main(String[] args) throws IOException {

    //TODO De temporal a archivo en directorio
    //TODO from file in temporary folder to file in common folder.
    File fromTemp_Dir = File.createTempFile("Temp_Dir_FROM", ".temp");
    File toTemp_Dir = new File ("Temp_Dir");        

    BufferedWriter bufferWriterTemp_DirFROM = new BufferedWriter(new FileWriter(fromTemp_Dir));
    bufferWriterTemp_DirFROM.write("Temp_Dir_FROM");
    bufferWriterTemp_DirFROM.close();

    BufferedWriter bufferWriterTemp_DirTO = new BufferedWriter(new FileWriter(toTemp_Dir));
    bufferWriterTemp_DirTO.write("Temp_Dir_TO");
    bufferWriterTemp_DirTO.close();

    System.out.println("fromTemp_Dir exist? "+fromTemp_Dir.exists());
    System.out.println("toTemp_Dir exist? "+toTemp_Dir.exists());


    toTemp_Dir.delete();
    if (!fromTemp_Dir.renameTo(toTemp_Dir)){
        System.out.println("rename fail 'De temporal a archivo en directorio'");
    }else {
        System.out.println("Rename successful 'De temporal a archivo en directorio'");
    } 


    //TODO De archivo a temporal
    //TODO from file in common folder to file in temporary folder.
    File fromDir_Temp = new File ("Dir_FROM");
    File toDir_Temp = File.createTempFile("Dir_Temp_TO", ".temp");

    BufferedWriter bufferWriterDir_TempFROM = new BufferedWriter(new FileWriter(fromDir_Temp));
    bufferWriterDir_TempFROM.write("Dir_Temp_FROM");
    bufferWriterDir_TempFROM.close();

    BufferedWriter bufferWriterDir_TempTO = new BufferedWriter(new FileWriter(toDir_Temp));
    bufferWriterDir_TempTO.write("Dir_Temp_TO");
    bufferWriterDir_TempTO.close();

    toDir_Temp.delete();
    if (!fromDir_Temp.renameTo(toDir_Temp)){
        System.out.println("rename fail 'De archivo en directorio a temporal'");
    } else{
        System.out.println("rename successful 'De archivo en directorio a temporal'");
    }


    //TODO De temporal a temporal
    //TODO from temporary folder to temporary folder
    File fromTemp = File.createTempFile("archivoTempFROM_", ".temp");
    File toTemp = File.createTempFile("archivoTempTO_", ".temp");

    BufferedWriter bufferWriterFROMTemp = new BufferedWriter(new FileWriter(fromTemp));
    bufferWriterFROMTemp.write("archivoTempFROM");
    bufferWriterFROMTemp.close();

    BufferedWriter bufferWriterTOTemp = new BufferedWriter(new FileWriter(toTemp));
    bufferWriterTOTemp.write("archivoTempTO");
    bufferWriterTOTemp.close();

    toTemp.delete();
    if (!fromTemp.renameTo(toTemp)){
        System.out.println("rename fail 'De temporal a temporal'");
    }else {
        System.out.println("Rename successful 'De temporal a temporal'");
    } 

    //TODO De archivo a archivo en directorio
    //TODO from file in common directory to file in common directory 
    File fromDir = new File("archivoDirFROM");
    File toDir = new File("archivoDirTO");

    BufferedWriter bufferWriterFROMDir = new BufferedWriter(new FileWriter(fromDir));
    bufferWriterFROMDir.write("archivoDirFROM");
    bufferWriterFROMDir.close();

    BufferedWriter bufferWriterTODir = new BufferedWriter(new FileWriter(toDir));
    bufferWriterTODir.write("archivoDirTO");
    bufferWriterTODir.close();

    toDir.delete();
    if (!fromDir.renameTo(toDir)){
        System.out.println("rename fail 'De archivo a archivo en directorio'");
    }else {
        System.out.println("Rename successful 'De archivo a archivo en directorio'");
    } 

}

Show in my console

fromTemp_Dir exist? true
toTemp_Dir exist? true
rename fail 'De temporal a archivo en directorio'
rename fail 'De archivo en directorio a temporal'
Rename successful 'De temporal a temporal'
Rename successful 'De archivo a archivo en directorio'

This class works on some PCs but not in other PC (tested in 7 PCs, 5 work, 2 not work), all with ubuntu installed.

Supposedly it is a permissions problem but I have set the PC as administrator and root.

I tried with gksu and sudo, but does not work.

Any solution?

Thanks.

2 Answers2

2

read the renameTo doc:

Whether or not this method can move a file from one filesystem to another is platform-dependent. The return value should always be checked to make sure that the rename operation was successful

I had somehow an issue like you do, but I had to work with a lot of files and I had the need to make pretty sure things happen. The solution was to use the OS to do that, calling Runtime.getRuntime().exec(command)

And inside command I could do a "mv" file1 file2 Sounds silly, but it did the job. Probably it is pretty expensive to do that too often, but it worked without any big problems.

Cheers, Eugene.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • This only works under linux OS but if I want to use windows OS does not work. I need to work on both Windows and Linux. – ipaddimm-75 Dec 23 '11 at 14:41
  • Well because Windows is.. well Windows and it uses some weird way of locking, you can't really force a rename. The file could be open, could be used by some anti-virus, some Windows Indexer, whatever and if it does you will not be able to rename it. You could implement a retry, but that is just a fancy way to run away from the problem. – Eugene Dec 23 '11 at 15:02
  • I have to further investigate how to solve this damn problem. thanks anyway. – ipaddimm-75 Dec 23 '11 at 17:02
1

Change the renameTo() method for

Files.move(new File(newFilePath), new File(oldFilePath));

this is working perfectly.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105