renameTo()在eclipse Helios中不起作用

时间:2011-12-23 12:41:03

标签: java eclipse temporary-files

这是一个简单的类来显示4个案例。 抱歉我的英语(很糟糕)

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'");
    } 

}

在我的控制台中显示

fromTemp_Dir存在吗?真正
toTemp_Dir存在?真正
重命名失败'De temporal a archivo en directorio'
重命名失败'De archivo en directorio a temporal' 重命名成功'De temporal a temporal' 重命名成功'De archivo a archivo en directorio'

这个类可以在某些PC上运行,但在其他PC上运行(在7台PC中测试,5个工作,2个不工作),所有都安装了ubuntu。

据说这是一个权限问题,但我已将PC设置为管理员和root。

我尝试使用gksu和sudo,但是没有用。

任何解决方案?

感谢。

2 个答案:

答案 0 :(得分:2)

阅读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

我有一个像你这样的问题,但我不得不处理很多文件,我需要确保事情发生。解决方案是使用操作系统来做,调用     调用Runtime.getRuntime()。EXEC(命令)

在命令内部我可以做一个“mv”file1 file2 听起来很傻,但它完成了这项工作。可能经常这样做是相当昂贵的,但它没有任何大问题。

干杯, 尤金。

答案 1 :(得分:1)

更改<{p>的renameTo()方法

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

这完美无缺。