简体   繁体   English

renameTo()在eclipse Helios中不起作用

[英]renameTo() not work in eclipse Helios

This is a simple class to show 4 cases. 这是一个简单的类,可以显示4种情况。 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? fromTemp_Dir存在吗? true 真正
toTemp_Dir exist? toTemp_Dir存在? 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' 重命名'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. 此类在某些PC上有效,但在其他PC上无效(已在7台PC上测试,其中5台可以工作,两台不工作),并且都安装了ubuntu。

Supposedly it is a permissions problem but I have set the PC as administrator and root. 据说这是一个权限问题,但我已将PC设置为管理员和root。

I tried with gksu and sudo, but does not work. 我试过gksu和sudo,但是没有用。

Any solution? 有解决方案吗

Thanks. 谢谢。

read the renameTo doc: 阅读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) 解决方案是使用操作系统来执行此操作,调用Runtime.getRuntime()。exec(command)

And inside command I could do a "mv" file1 file2 Sounds silly, but it did the job. 在内部命令我可以做一个“mv”file1 file2听起来很傻,但它完成了这项工作。 Probably it is pretty expensive to do that too often, but it worked without any big problems. 可能经常这样做是相当昂贵的,但它没有任何大问题。

Cheers, Eugene. 干杯,尤金。

Change the renameTo() method for 更改renameTo()方法

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

this is working perfectly. 这是完美的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM