博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA FILE.renameTo跨文件系统移动文件失败
阅读量:7099 次
发布时间:2019-06-28

本文共 4922 字,大约阅读时间需要 16 分钟。

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

FILE.renameTo接口说明:

public boolean renameTo( 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 class defines the 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:
- If a security manager exists and its
method denies write access to either the old or new pathnames
- If parameter
dest is
null

Files.move接口说明:

public static  move( source,         target,        ... options)                 throws
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 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 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 method in conjunction with the utility method.

The options parameter may include any of the following:

Option Description
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.
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 . If the move cannot be performed as an atomic file system operation then 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 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:
- if the array contains a copy option that is not supported
- if the target file exists but cannot be replaced because the
REPLACE_EXISTING option is not specified
(optional specific exception)
- the
REPLACE_EXISTING option is specified but the file cannot be replaced because it is a non-empty directory
(optional specific exception)
- if the options array contains the
ATOMIC_MOVE option but the file cannot be moved as an atomic file system operation.
- if an I/O error occurs
- In the case of the default provider, and a security manager is installed, the method is invoked to check write access to both the source and target file.

转载于:https://www.cnblogs.com/cloudwind2011/p/9525288.html

你可能感兴趣的文章
如何更深入地学习Linux?
查看>>
目标检測的图像特征提取之(一)HOG特征
查看>>
MySQL-EXPLAIN用法详解
查看>>
jdbctemplate中的query(sql,params,mapper)与queryForList(sql,params,class)区别
查看>>
C++ 虚函数表解析
查看>>
Responder一点也不神秘————iOS用户响应者链完全剖析
查看>>
Type mismatch: cannot convert from java.sql.PreparedStatement to com.mysql.jdbc.PreparedStatement
查看>>
thinkphp 重定向redirect
查看>>
Builder创建者模式
查看>>
安卓应用使用QQ登录的申请流程
查看>>
Android批量图片加载经典系列——采用二级缓存、异步加载网络图片
查看>>
redis 数据类型详解 以及 redis适用场景场合
查看>>
RAC安装重新运行root.sh
查看>>
Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.2(转)
查看>>
Java多线程有哪几种实现方式? Java中的类如何保证线程安全? 请说明ThreadLocal的用法和适用场景...
查看>>
工作队列(workqueue) create_workqueue/schedule_work/queue_work
查看>>
size_t、ptrdiff_t【转】
查看>>
Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 ...
查看>>
【Linux】查看所使用的Linux系统是32位还是64 位的方法
查看>>
NSJSONSerialization 反序列化失败 NSCocoaErrorDomain Code=3840
查看>>