0

我正在尝试使用 java 中的 renameTo() 将文件从一个目录移动到另一个目录,但是 renameTo 不起作用(不重命名和移动文件)。基本上,我想先删除一个具有相同文件名的文件,然后将文件从另一个目录复制到我最初删除文件的相同位置,然后复制具有相同名称的新文件。

    //filePath = location of original file with file name appended. ex: C:\Dir\file.txt
    //tempPath = Location of file that I want to replace it to file file without the file name.  ex: C:\AnotherDir

    int pos = filePath.indexOf("C:\\Dir\\file.txt");
    //Parse out only the path, so just C:\\Dir
    String newFilePath = filePath.substring(0,pos-1);

    //I want to delete the original file
    File deletefile = new File(newFilePath,"file.txt");

    if (deletefile.exists()) {
        success = deletefile.delete();
    }


    //There is file already exists in the directory, but I am just appending .tmp at the end
    File newFile = new File(tempPath + "file.txt" + ".tmp");

    //Create original file again with same name.
    File oldFile = new File(newFilePath, "file.txt");

    success = oldFile.renameTo(newFile); // This doesnt work.

你能告诉我我做错了什么吗?

谢谢你的帮助。

4

2 回答 2

5

您需要转义字符串文字中的反斜杠:"C:\\Dir\\file.txt"。或用于File.separator构造路径。

此外,确保newFile正确构建路径:

File newFile = new File(tempPath + File.separator + "file.txt" + ".tmp");
                               //^^^^^^^^^^^^^^^^

由于已发布代码 ( ...ex: C:\AnotherDir) 中的注释表明tempPath没有尾部斜杠字符。

于 2012-09-27T19:41:23.110 回答
0

我已将文件移动到目标目录,并在移动后以三种方式从源文件夹中删除了这些移动的文件,最后在我的项目中使用了第三种方法。

第一种方法:

File folder = new File("SourceDirectory_Path");
    File[] listOfFiles = folder.listFiles();
    for (int i = 0; i < listOfFiles.length; i++) {
    Files.move(Paths.get("SourceDirectory_Path"+listOfFiles[i].getName()), Paths.get("DestinationDerectory_Path"+listOfFiles[i].getName()));
    }
    System.out.println("SUCCESS");

第二种方法:

 Path sourceDir = Paths.get("SourceDirectory_Path");
    Path destinationDir = Paths.get("DestinationDerectory_Path");
      try(DirectoryStream<Path> directoryStream =   Files.newDirectoryStream(sourceDir)){
        for (Path path : directoryStream) {
            File d1 = sourceDir.resolve(path.getFileName()).toFile();
             File d2 = destinationDir.resolve(path.getFileName()).toFile(); 
             File oldFile = path.toFile();
            if(oldFile.renameTo(d2)){
                System.out.println("Moved");
            }else{
                System.out.println("Not Moved");
            }
        }
    }catch (Exception e) {
        e.printStackTrace();
    }

第三种方法:

Path sourceDirectory= Paths.get(SOURCE_FILE_PATH);
            Path destinationDirectory = Paths.get(SOURCE_FILE_MOVE_PATH);
            try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(sourceDirectory)) {
                for (Path path : directoryStream) {                                    
                    Path dpath = destinationDirectory .resolve(path.getFileName());                                    
                    Files.move(path, dpath, StandardCopyOption.REPLACE_EXISTING);
                }
            } catch (IOException ex) {
                ex.printStackTrace();   
            }

快乐编码!:)

于 2015-08-21T06:28:35.437 回答