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 ...
随机推荐
- hdu 3068 最长回文_Manacher模板
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/neng18/article/details/24269469 pid=3068" rel= ...
- UVa 10791 - Minimum Sum LCM(唯一分解定理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- HDU 3292 【佩尔方程求解 && 矩阵快速幂】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...
- HDU 6395 Sequence 【矩阵快速幂 && 暴力】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) ...
- LwIP协议栈开发嵌入式网络的三种方法分析
LwIP协议栈开发嵌入式网络的三种方法分析 摘要 轻量级的TCP/IP协议栈LwIP,提供了三种应用程序设计方法,且很容易被移植到多任务的操作系统中.本文结合μC/OS-II这一实时操作系统,以 ...
- Jumpserver堡垒机搭建(脚本自动化)
#!/bin/bash # coding: utf- # Copyright (c) set -e #返回值为非0时,退出脚本 echo "0. 系统的一些配置" setenfor ...
- 缓存反向代理-Varnish
简介 Varnish是一款高性能.开源的缓存反向代理服务器.它从客户端接受请求,并尝试从缓存中响应请求,如果无法从缓存中提供响应,Varnish 向后端服务器发起请求,获取响应,将响应存储在缓存中,然 ...
- ffmreg thinkphp 控制器 获取音频视频详细信息(获取时长)
FFmpeg下载:http://ffmpeg.zeranoe.com/builds/ 下载并解压FFmpeg文件夹: 打开你想安装的任意磁盘,例如:d盘.新建一个名为“ffmpeg”的文件夹,将第二步 ...
- Oracle之单表查询及常用函数
1.语法: select 字段列表 from 表名 [where 查询条件] [group by 分组] [having 分组条件] [order by 排序] select * 代表查询所有的字段 ...
- Oracle之基础操作
sqlplus常用命令: 进入sqlplus模式:sqlplus /nolog 管理员登录: conn / as sysdba 登录本机的数据库 conn sys/123456 as sysdba 普 ...