嗨,那天一个群里有人问 :
文件复制怎么实现
工作上处于测试阶段,不太忙,就总结一下。记录下来分享一下!
目录
1. 传统1.1 最基础的复制 字节流(字符流同理)1.2 缓冲流使用 字节流2. NIO2.1 nio 通道 非直接缓冲2.2 nio 通道 直接缓冲2.3 通道直接3. 附上调用代码总结与联想
1. 传统
1.1 最基础的复制 字节流(字符流同理)
1/**
2 * @param
3 * @return
4 * @description 最基础的复制 字节流(字符流同理)
5 * @author: xiaoying@hexindai.com V1.0 2018/6/13
6 */
7 public static void copyBase(String srcPath, String destPath) {
8
9 FileInputStream fis = null;
10 FileOutputStream fos = null;
11 try {
12 fis = new FileInputStream(srcPath);
13 fos = new FileOutputStream(destPath);
14 byte[] buf = new byte[1024];
15 int len = 0;
16 while ((len = fis.read(buf)) != -1) {
17 fos.write(buf, 0, len);
18 }
19 } catch (IOException e) {
20 e.printStackTrace();
21 } finally {
22 if (fos != null) {
23 try {
24 fos.close();
25 } catch (IOException e) {
26 e.printStackTrace();
27 }
28 }
29 if (fis != null) {
30 try {
31 fis.close();
32 } catch (IOException e) {
33 e.printStackTrace();
34 }
35 }
36 }
37 }
1.2 缓冲流使用 字节流
1 /**
2 * @param
3 * @return
4 * @description 缓冲流使用 字节流
5 * @author: xiaoying@hexindai.com V1.0 2018/6/13
6 */
7 public static void copyBuffer(String srcPath, String destPath) {
8
9 BufferedInputStream bis = null;
10 BufferedOutputStream bos = null;
11 try {
12 bis = new BufferedInputStream(new FileInputStream(srcPath));
13 bos = new BufferedOutputStream(new FileOutputStream(destPath));
14 int len = 0;
15 byte[] buf = new byte[1024];
16 while ((len = bis.read(buf)) != -1) {
17 bos.write(buf, 0, len);
18 //bos.flush();
19 }
20 } catch (IOException e) {
21 e.printStackTrace();
22 } finally {
23 if (bis != null) {
24 try {
25 bis.close();
26 } catch (IOException e) {
27 e.printStackTrace();
28 }
29 }
30 if (bos != null) {
31 try {
32 bos.close();
33 } catch (IOException e) {
34 e.printStackTrace();
35 }
36 }
37 }
38 }
2. NIO
2.1 nio 通道 非直接缓冲
1/**
2 * @param
3 * @return
4 * @description nio 通道 非直接缓冲
5 * @author: xiaoying@hexindai.com V1.0 2018/6/13
6 */
7 public static void copyInDirectChannel(String srcPath, String destPath) throws Exception {
8
9 FileInputStream fis = new FileInputStream(srcPath);
10 FileOutputStream fos = new FileOutputStream(destPath);
11
12 //获取通道
13 FileChannel inChannel = fis.getChannel();
14 FileChannel outChannel = fos.getChannel();
15
16 //获取缓冲器
17 ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
18 while (inChannel.read(byteBuffer) != -1) {
19 byteBuffer.flip();//切换读模式
20 outChannel.write(byteBuffer);//写
21 byteBuffer.clear();//重置
22 }
23 outChannel.close();
24 inChannel.close();
25 fos.close();
26 fis.close();
27 }
2.2 nio 通道 直接缓冲
1/**
2 * @param
3 * @return
4 * @throws Exception 应该try catch 简化代码
5 * @description * nio 通道 直接缓冲
6 * @author: xiaoying@hexindai.com V1.0 2018/6/13
7 */
8 public static void copyDirectChannel(String srcPath, String destPath) throws Exception {
9
10 //获取通道
11 FileChannel inChannel = FileChannel.open(Paths.get(srcPath), StandardOpenOption.READ);
12 FileChannel outChannel = FileChannel.open(Paths.get(destPath), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
13
14 //内存映射文件
15 MappedByteBuffer inMappedByteBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0L, inChannel.size());
16 MappedByteBuffer outMappedByteBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE, 0L, inChannel.size());
17
18 //直接读写
19 byte[] buf = new byte[inMappedByteBuffer.limit()];
20 inMappedByteBuffer.get(buf);
21 outMappedByteBuffer.put(buf);
22
23 outChannel.close();
24 inChannel.close();
25 }
2.3 通道直接
1 /**
2 * @param
3 * @return
4 * @description 通道直接
5 * @author: xiaoying@hexindai.com V1.0 2018/6/13
6 */
7 public static void copyDirectOfChannel(String srcPath, String destPath) throws Exception {
8
9 //获取通道
10 //Files.newByteChannel(Paths.get(srcPath),StandardOpenOption.READ);获取方式也可以
11 FileChannel inChannel = FileChannel.open(Paths.get(srcPath), StandardOpenOption.READ);
12 FileChannel outChannel = FileChannel.open(Paths.get(destPath), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
13
14 //二选其一
15// inChannel.transferTo(0, inChannel.size(), outChannel);
16 outChannel.transferFrom(inChannel, 0, inChannel.size());
17
18 inChannel.close();
19 outChannel.close();
20 }
3. 附上调用代码
1 public static void main(String[] args) throws Exception {
2
3 String srcPath = "C:\\1.pdf";
4 String destPath = "D:\\11111.pdf";
5// CopyFileTest.copyBase(srcPath,destPath);
6// CopyFileTest.copyBuffer(srcPath, destPath);
7// copyDirectChannel(srcPath,destPath);
8// copyInDirectChannel(srcPath,destPath);
9 copyDirectOfChannel(srcPath, destPath);
10 }
总结与联想
是否想到socket编程,是否想到目前比较火的Netty(目前不会)?
2.是否想到项目常用的IO业务场景上传与下载?
3.是否想到Google的Thumbnailator图片处理工具?
4.是否看了看IO的源码 比如 单例,建造,工厂,装饰等模式?
5.是否想到了Lambeda(拼音不一定对)表达式
6.是否想到了枚举
7.是否想到了单元测试,业务接口测试
IO操作,搞清输入对象和输出对象,按照某种方式(字节字符处理) 是否依赖管道(性能),发出与接收(节点)。完活。
实际业务场景很少有IO操作(都是大神们封装好的)。
如看不懂,可以咨询我,也可以自己研究一下。端午节快乐,做自己想做的事!!!
coding
时,做到了如何落实
;writing
时,做到了如何表达
;sharing
时,做到了如何传授
;