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 ...
随机推荐
- 2spring注解:@Lazy,@Conditional,@import,FactoryBean接口
大致的工程内容也来源于上一篇博文! https://www.cnblogs.com/Mrchengs/p/10108603.html 1.@Lazy懒加载 懒加载:针对单实例的 单实例bean,默认在 ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- 2019.1.9 Mac安装Iterm2 终端(oh my zsh的安装与配置)
Mac安装Iterm2 终端(oh my zsh的安装与配置) 安装 curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tool ...
- Java 和 .NET SHA1算法记录
最近做了一个.NET访问Java接口的小Demo,其中用到了SHA1加密,大体思路就是.NET 传一些参数然后SHA1加密,Java端接收到之后在SHA1加密对比. Java代码: public fi ...
- 可枚举接口的知识点(IEnumerable 接口)要使用foreach,就必须实现可枚举接口
- Pandas快速入门(深度学习入门2)
源地址为:http://pandas.pydata.org/pandas-docs/stable/10min.html#min Pandas(Python Data Analysis Library) ...
- 用条件变量实现事件等待器的正确与错误做法--转自陈硕的Blog
用条件变量实现事件等待器的正确与错误做法 TL;DR 如果你能一眼看出 https://gist.github.com/chenshuo/6430925 中的那 8 个 Waiter classes ...
- Python 学习笔记(十一)Python语句(二)
For 循环语句 基础知识 for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for 循环规则: do sth >>> for i in "python ...
- oracle静默安装
RHEL6+oracle11.2 无界面化命令安装如下: 1.所需安装软件包检查: yum install binutils-2.* compat-libcap1* compat-libstdc++- ...
- 团体队列 UVA540 Team Queue
题目描述 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么新人会插队到最后一个队友的身后.如果没有任何一个队友排队,则他会被排到长队的队尾. 输入每个团队中所有队员的编号,要求 ...