JAVA FILE.renameTo跨文件系统移动文件失败

遇到了FILE.renameTo跨文件系统移动文件失败的问题,应使用FILES.move()接口或在同一文件系统移动文件。

FILE.renameTo接口说明:

public boolean renameTo(File dest)

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

Note that the Files class defines the move method to move or rename a file in a platform independent manner.

Parameters:
dest - The new abstract pathname for the named file
Returns:
true if and only if the renaming succeeded; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to either the old or new pathnames
NullPointerException - If parameter dest is null

Files.move接口说明:

public static Path move(Path source,
        Path target,
        CopyOption... options)
                 throws IOException

Move or rename a file to a target file.

By default, this method attempts to move the file to the target file, failing if the target file exists except if the source and target are the same file, in which case this method has no effect. If the file is a symbolic link then the symbolic link itself, not the target of the link, is moved. This method may be invoked to move an empty directory. In some implementations a directory has entries for special files or links that are created when the directory is created. In such implementations a directory is considered empty when only the special entries exist. When invoked to move a directory that is not empty then the directory is moved if it does not require moving the entries in the directory. For example, renaming a directory on the same FileStore will usually not require moving the entries in the directory. When moving a directory requires that its entries be moved then this method fails (by throwing an IOException). To move a file tree may involve copying rather than moving directories and this can be done using the copy method in conjunction with the Files.walkFileTree utility method.

The options parameter may include any of the following:

Option Description
REPLACE_EXISTING If the target file exists, then the target file is replaced if it is not a non-empty directory. If the target file exists and is a symbolic link, then the symbolic link itself, not the target of the link, is replaced.
ATOMIC_MOVE The move is performed as an atomic file system operation and all other options are ignored. If the target file exists then it is implementation specific if the existing file is replaced or this method fails by throwing an IOException. If the move cannot be performed as an atomic file system operation then AtomicMoveNotSupportedException is thrown. This can arise, for example, when the target location is on a different FileStore and would require that the file be copied, or target location is associated with a different provider to this object.

An implementation of this interface may support additional implementation specific options.

Where the move requires that the file be copied then the last-modified-time is copied to the new file. An implementation may also attempt to copy other file attributes but is not required to fail if the file attributes cannot be copied. When the move is performed as a non-atomic operation, and a IOException is thrown, then the state of the files is not defined. The original file and the target file may both exist, the target file may be incomplete or some of its file attributes may not been copied from the original file.

Usage Examples: Suppose we want to rename a file to "newname", keeping the file in the same directory:

     Path source = ...
     Files.move(source, source.resolveSibling("newname"));
 

Alternatively, suppose we want to move a file to new directory, keeping the same file name, and replacing any existing file of that name in the directory:

     Path source = ...
     Path newdir = ...
     Files.move(source, newdir.resolve(source.getFileName()), REPLACE_EXISTING);
 
Parameters:
source - the path to the file to move
target - the path to the target file (may be associated with a different provider to the source path)
options - options specifying how the move should be done
Returns:
the path to the target file
Throws:
UnsupportedOperationException - if the array contains a copy option that is not supported
FileAlreadyExistsException - if the target file exists but cannot be replaced because the REPLACE_EXISTING option is not specified (optional specific exception)
DirectoryNotEmptyException - the REPLACE_EXISTING option is specified but the file cannot be replaced because it is a non-empty directory (optional specific exception)
AtomicMoveNotSupportedException - if the options array contains the ATOMIC_MOVE option but the file cannot be moved as an atomic file system operation.
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to both the source and target file.

原文地址:https://www.cnblogs.com/cloudwind2011/p/9525288.html

时间: 2024-08-30 01:38:42

JAVA FILE.renameTo跨文件系统移动文件失败的相关文章

java移动文件夹、 慎用java file.renameTo(f)方法 、 java从一个目录复制文件到另一个目录下 、 java代码完成删除文件、文件夹 、

java移动文件夹(包含子文件和子文件夹): http://blog.csdn.net/yongh701/article/details/45070353 慎用java    file.renameTo(f)方法: http://www.cnblogs.com/mrwangblog/p/3934506.html 注意看结果,从C盘到E盘失败了,从C盘到D盘成功了.因为我的电脑C.D两个盘是NTFS格式的,而E盘是FAT32格式的.所以从C到E就是上面文章所说的"file systems"

java 的 renameTo方法重命名失败

java 的 renameTo方法重命名文件失败 ,没有异常: 解决方案,应该是重命名之前你在读它 类似如下,先重命名 ,但这个时候你还在读取它,流没有关闭呢. 这个问题很难发现因为,java没有报任何异常

java file文件类操作使用方法大全

1.构造函数 [java] view plaincopy public class FileDemo { public static void main(String[] args){ //构造函数File(String pathname) File f1 =new File("c:\\zuidaima\\1.txt"); //File(String parent,String child) File f2 =new File("c:\\zuidaima",&quo

Java File类方法使用详解

Java File类的功能非常强大,利用java基本上可以对文件进行所有操作.文本将对Java File 文件操作的类详细的分析,并将File类中的常用方法进行简单介绍. 构造函数 public class FileDemo { public static void main(String[] args) { //构造函数File(String pathname) File f1 = new File("D:\\a\\1.txt"); //File(String parent,Stri

java——File类的用法整理

参考:http://www.codeceo.com/article/java-file-class.html 构造函数 public class FileDemo { public static void main(String[] args){ //构造函数File(String pathname) File f1 =new File("c:\\abc\\1.txt"); //File(String parent,String child) File f2 =new File(&qu

java File类用法

构造函数 复制代码代码如下: public class FileDemo {     public static void main(String[] args){         //构造函数File(String pathname)         File f1 =new File("c:\\abc\\1.txt");         //File(String parent,String child)         File f2 =new File("c:\\ab

请慎用java的File#renameTo(File)方法(转)

以前我一直以为File#renameTo(File)方法与OS下面的 move/mv 命令是相同的,可以达到改名.移动文件的目的.不过后来经常发现问题:File#renameTo(File)方法会返回失败(false),文件没有移动,又查不出原因,再后来干脆弃用该方法,自己实现一个copy方法,问题倒是再也没有出现过. 昨天老板同学又遇到这个问题,File#renameTo(File)方法在windows下面工作的好好的,在linux下偶尔又失灵了.回到家我扫了一遍JDK中File#rename

Java File类(文件的读取,写入,复制与重命名)

文件的重命名   file.reNameTo() public boolean renameTo(File dest) 重新命名此抽象路径名表示的文件. 此方法行为的许多方面都是与平台有关的:重命名操作无法将一个文件从一个文件系统移动到另一个文件系统,dest为新命名的抽象文件 public boolean ReName(String path,String newname) {//文件重命名 //Scanner scanner=new Scanner(System.in); File file

Oracle工具之--ASM与文件系统及跨网络传输文件

Oracle工具之--ASM与文件系统及跨网络传输文件   Oracle DBMS_FILE_TRANSFER可以实现文件系统和ASM磁盘组之间实现文件传输及ASM磁盘组之间跨网络的传输. DBMS_FILE_TRANSFER:   The DBMS_FILE_TRANSFER package provides procedures to copy a binary file within a database or to transfer a binary file between datab