import static java.nio.file.StandardOpenOption.*;
import java.nio.file.*;
import java.nio.channels.*;
import java.io.IOException;
import java.util.EnumSet;
public class FileBackup {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Path file = Paths.get(System.getProperty("user.home")).resolve(
"Beginning Java Stuff").resolve("Sayings.txt");
if (!Files.exists(file)) {
System.err.println(file + " is not exist.");
System.exit(1);
}
file.toAbsolutePath();//将路径转化为绝对路径,如果已经是绝对路径则原样返回
Path tofile= createBackupFilePath(file);//创建复制文件路径
try{
FileChannel inCh=(FileChannel)(Files.newByteChannel(file));//filechannel具有更高的功能
WritableByteChannel outCh=Files.newByteChannel(tofile,EnumSet.of(WRITE,CREATE));//新建写入文件通道
int byteWritten=0;
long byteCount=inCh.size();
while(byteWritten<byteCount){
byteWritten+=inCh.transferTo(byteWritten,byteCount-byteWritten,outCh);
//Transfers bytes from this channel's file to the given writable byte channel.
//long transferTo(long position,long count,WritableByteChannel target) position是开始位置,count是要读取的字节数,target是目标通道
//返回的是实际转化的字节数
}
System.out.printf("File copy complete. %d bytes copied to %s%n", byteCount, tofile);
}catch(IOException e){
e.printStackTrace();
}
}
public static Path createBackupFilePath(Path file){
Path parent=file.getParent();
String name=file.getFileName().toString();//getFilename返回文件名(路径类型),也就是路径中距离根目录最远的
int period=name.indexOf('.');
if(period==-1){
period=name.length();
}
String nameAdd="_backup";
Path backup=parent.resolve(name.substring(0,period)+nameAdd+name.substring(period));//修改路径,建立复制文件路径
while(Files.exists(backup)){//检测新建的路径是否存在,如果存在则继续在文件名后面加_backup
name=backup.getFileName().toString();
backup=parent.resolve(name.substring(0,period)+nameAdd+name.substring(period));
period+=nameAdd.length();
}
return backup;
}
}
相关推荐
1、配置文件上传和下载的相关...通过transferTo()方法将文件保存到指定的路径。 4、实现文件下载功能:在文件下载的方法中,可以使用ResponseEntity作为返回类型,并设置响应头信息,将文件以流的形式写入响应体中。
通过springmvc5.3.16+layui2.8.11版本实现文件上传及...上传文件之后将上传成功的文件从页面删除,通过CommonsMultipartResolver和MultipartHttpServletRequest关键类实现多文件的捕获,通过transferTo实现文件的上传。
cjson库文件的使用,方便c将数据以json格式传递,web前端解析json格式数据
图片上传文件上传。封装好了方法,只需要引用方法就可以上传图片文件,返回文件大小,文件类型
主要介绍了Java中IO字节流基本操作(复制文件)并测试性能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
MultipartFile.transferto() 多个文件上传 第二次调用 会报错(系统找不到文件,临时文件被删除)
一. 管道 1.transferFrom:从指定管道获取数据 2.transferTo:推给指定管道数据 二. 传统文件拷贝
compress file in ZIP with delphi and transfer to a location
文件上传下载 根据multipart类的方法: 即multipartFile.transferTo(file); 就完成文件上传
使用FileChannel#transferTo而不使用String比较。 安装 可从。 repositories { jcenter() } compile 'net.ypresto.qtfaststartjava:qtfaststart:0.1.0' 执照 MIT许可证。 有关原始源代码的信息 该产品基于qt-
摘要我们展了transferTo传统式上的性能优势,在从个channel读取相同数据发送到另个channel的操作上。如果有个需要在channel间量复制数据的
files.get(i).transferTo(new File(path+File.separator+filename)); //获取数据库存储路径 String root = request.getContextPath(); String mkdirsql = hex.charAt(0)+"/"+hex.charAt(1)+"/"; ...
首先,kafka的开发者们认为不需要在内存里缓存什么数据,操作系统的文件缓存已经足够完善和强大,只要你不搞随机写,顺序读写的性能是非常高效的。kafka的数据只会顺序append,数据的删除策略是累积到一定程度或者...
6470086: (fc) FileChannel.transferTo(2147483647, 1, channel) cause "Value too large" exception ============================= Enabling SSI and CGI Support: ============================= Because ...