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 ...
随机推荐
- ZooKeeper介绍与环境搭建
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...
- boost::timer库使用
boost::timer boost库定时器使用,需要在编译时加相关链接库 -lboost_timer -lboost_system boost::timer::cpu_timer 和boost::t ...
- 关于PHP上传文件失败但是找不到原因的问题?
确定上传文件的前后台代码都准确无误后,可以考虑是不是PHP配置文件中限制了上传文件的大小 1.找到根目录中的PHP文件夹 2.点击当前正在使用的的PHP版本 3.搜索php.ini 4.打开 phpF ...
- thinkphp5.0查询到的数据表中的路径是反斜杠导致无法正常显示图片怎么办?
添加到数据表中图片的路径有时会是反斜杠,这就导致了在url后面写路径的时候会识别不出来(不过src后面写路径就可以识别),所以就需要把路径中的反斜杠替换成正斜杠,代码如下: $datu = Db::q ...
- [19/03/25-星期一] 容器_Collection(集合、容器)之Set(集合、安置,无顺序不可重复)
一.概念&方法 Set接口继承自Collection,Set接口中没有新增方法,方法和Collection保持完全一致.. Set容器特点:无序.不可重复.无序指Set中的元素没有索引,只能遍 ...
- [转]java中文乱码的解决
在基于Java的编程中,经常会碰到汉字的处里及显示的问题,比如一大堆乱码或问号. 这是因为JAVA中默认的编码方式是UNICODE,而中国人通常使用的文件和DB都是基于GB2312或者BIG5等编码, ...
- Segmentation fault(Core Dump)
Segmentation fault 这个提示还是比较常见的,这个提示就是段错误,这是翻译还是十分恰当的. Core Dump 有的时候给我们呈现的翻译很有趣是”吐核“,但是实际上比较贴切的翻译是核心 ...
- EF Core中关于System.Linq.Dynamic.Core的使用(转载)
项目中经常用到组合条件查询,根据用户配置的查询条件进行搜索,拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦.有个System.Linq.Dynamic.Core用起来比 ...
- 解决MyEclipse JAVA EE无法识别Base64问题
第一步:右击项目选择Build Path,选择Configure Build Path 第二步:点击JRE System Library选择右边的Edit 第三步:选择Alternate JRE,点击 ...
- Jumpserver堡垒机搭建(脚本自动化)
#!/bin/bash # coding: utf- # Copyright (c) set -e #返回值为非0时,退出脚本 echo "0. 系统的一些配置" setenfor ...