JAVA FILE.renameTo跨文件系统移动文件失败
遇到了FILE.renameTo跨文件系统移动文件失败的问题,应使用FILES.move()接口或在同一文件系统移动文件。
FILE.renameTo接口说明:
public boolean renameTo(File dest)
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:
trueif and only if the renaming succeeded;falseotherwise- Throws:
SecurityException- If a security manager exists and itsmethod denies write access to either the old or new pathnamesSecurityManager.checkWrite(java.lang.String)NullPointerException- If parameterdestisnull
Files.move接口说明:
-
public static Path move(Path source,
Path target,
CopyOption... options)
throws IOExceptionMove 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
samefile, 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 sameFileStorewill 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 anIOException). To move a file tree may involve copying rather than moving directories and this can be done using thecopymethod in conjunction with theFiles.walkFileTreeutility method.The
optionsparameter may include any of the following:Option Description REPLACE_EXISTINGIf 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_MOVEThe 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 thenAtomicMoveNotSupportedExceptionis thrown. This can arise, for example, when the target location is on a differentFileStoreand 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-timeis 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 aIOExceptionis 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 movetarget- 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 supportedFileAlreadyExistsException- if the target file exists but cannot be replaced because theREPLACE_EXISTINGoption is not specified (optional specific exception)DirectoryNotEmptyException- theREPLACE_EXISTINGoption is specified but the file cannot be replaced because it is a non-empty directory (optional specific exception)AtomicMoveNotSupportedException- if the options array contains theATOMIC_MOVEoption but the file cannot be moved as an atomic file system operation.IOException- if an I/O error occursSecurityException- In the case of the default provider, and a security manager is installed, thecheckWritemethod is invoked to check write access to both the source and target file.
JAVA FILE.renameTo跨文件系统移动文件失败的更多相关文章
- java中File的delete()方法删除文件失败的原因
java中File的delete()方法删除文件失败的原因 学习了:http://hujinfan.iteye.com/blog/1266387 的确是忘记关闭了: 引用原文膜拜一下: 一般来说 ja ...
- 转!!java中File的delete()方法删除文件失败的原因
一般来说 java file.delete失败 有以下几个原因 1.看看是否被别的进程引用,手工删除试试(删除不了就是被别的进程占用)2.file是文件夹 并且不为空,有别的文件夹或文件, 3.极有可 ...
- Java File.renameTo方法的问题
今天发现一个问题,renameTo执行失败. 程序是这样的:一个小程序在执行完成时会将A目录的文件renameTo到B目录,该程序一直运行正常.今天将B目录进行了mount挂载(Linux上),挂载后 ...
- Java File创建新目录和文件
创建目录 当不存在目录aa文件夹时: File file1=new File("/aa"); Boolean aa=file.mkdir();// true File file1= ...
- Java File 类的使用方法详解
Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对Java File文件操作类进行详细地分析,并将File类中的常用方法进行简单介绍,有需要的Java开发者可以看 ...
- Java File 类的使用方法详解(转)
转自:http://www.codeceo.com/article/java-file-class.html Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对J ...
- Java File类方法使用详解
Java File类的功能非常强大,利用java基本上可以对文件进行所有操作.文本将对Java File 文件操作的类详细的分析,并将File类中的常用方法进行简单介绍. 构造函数 public cl ...
- 请慎用java的File#renameTo(File)方法(转)
以前我一直以为File#renameTo(File)方法与OS下面的 move/mv 命令是相同的,可以达到改名.移动文件的目的.不过后来经常发现问题:File#renameTo(File)方法会返回 ...
- Java实现FTP跨服务器文件操作
在过去的几年工作中,曾经多次需要把文件上传到单独的服务器,而程序是在单独的服务器上部署的,在进行文件操作的时候就需要跨服务器进行操作包括:文件上传.文件下载.文件删除等.跨服务器文件操作一般是需要FT ...
随机推荐
- 1.Jdeveloper打印出完整日志(-Djbo.debugoutput=console)
有时候在JDeveloper中需要打印出来比较系统和完整的ADF运行时日志 例如,想查看VO当前执行的是哪个View Criteria,运行的完整SQL语句到底如何 以及当前Binding Varia ...
- ssm小小整合
ssm整合: 首先是创建数据库: CREATE TABLE `user` ( `id` ) NOT NULL, `username` varchar() NOT NULL, `password` va ...
- PowerShell交互下的热键
- Mybatis 和Spring整合之原始dao开发
F:\Aziliao\mybatis\代码\31.mybatis与spring整合-开发原始dao 1.1. SqlMapConfig.xml <?xml version="1.0&q ...
- vue中调用地图
一. vue-amap,一个基于 Vue 2.x 和高德地图的地图组件 这个就不细说了,按照其文档,就能够安装下来. 二. 按照官方提供的方法引入 1.修改webpac.base.conf.js文件 ...
- PL\SQL设置中文
打开PL\SQL 1.菜单->Tools ->preferences->User Interface -> Appearance -> Language 选择 Chine ...
- HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...
- jquery固定位置浮动
示例: <!DOCTYPE html> <html> <head> <title>test page</title> <script ...
- WebPack系列之Proxy
背景 当我们直接去访问https://m.weibo.cn/api/config/list时由于端口.域名的不同产生了跨域,无法请求到资源数据. 分析 WebPack中devServer的proxy代 ...
- Yii2中使用Soap WebSerivce
Soap是一种轻量的.简单的.基于XML(标准通用标记语言下的一个子集)的协议 WebService顾名思义就是web服务,web服务主要有两种,一种是基于soap类型的服务,一种是基于rest类型的 ...