java nio 复制文件功能
来源:互联网 发布:阿里云虚拟主机安装web 编辑:程序博客网 时间:2024/05/23 19:06
- package com.ghb.crp.file;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- public class FileCopy {
- public static String src = "f://ss.rmvb";
- public static String dist = "f://ss2.rmvb";
- /**
- * @param args
- */
- public static void main(String[] args) {
- long start = System.currentTimeMillis();
- copyNew();
- long end = System.currentTimeMillis();
- System.out.println("nio 用时: " + (end - start));
- long start2 = System.currentTimeMillis();
- copyOld();
- end = System.currentTimeMillis();
- System.out.println("io 用时: " + (end - start2));
- }
- public static void copyOld() {
- File srcFile = new File(src);
- File distFile = new File(dist);
- if (distFile.exists()) {
- distFile.delete();
- }
- try {
- FileInputStream fin = new FileInputStream(srcFile);
- BufferedInputStream buffIn = new BufferedInputStream(fin);
- FileOutputStream fout = new FileOutputStream(distFile);
- BufferedOutputStream buffout = new BufferedOutputStream(fout);
- byte[] bytes = new byte[1024];
- while (buffIn.read(bytes) > 0) {
- buffout.write(bytes);
- }
- fin.close();
- buffIn.close();
- fout.close();
- buffout.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void copyNew() {
- try {
- File srcFile = new File(src);
- File distFile = new File(dist);
- if (distFile.exists()) {
- distFile.delete();
- }
- FileInputStream fin = new FileInputStream(srcFile);
- FileOutputStream fout = new FileOutputStream(distFile);
- FileChannel inChannel = fin.getChannel();
- FileChannel outChannel = fout.getChannel();
- int ByteBufferSize = 1024 * 100;
- ByteBuffer buff = ByteBuffer.allocate(ByteBufferSize);
- while (inChannel.read(buff) > 0) {
- buff.flip();
- if (inChannel.position() == inChannel.size()) {// 判断是不是最后一段数据
- int lastRead = (int) (inChannel.size() % ByteBufferSize);
- byte[] bytes = new byte[lastRead];
- buff.get(bytes, 0, lastRead);
- outChannel.write(ByteBuffer.wrap(bytes));
- buff.clear();
- } else {
- outChannel.write(buff);
- buff.clear();
- }
- }// 这个使用FileChannel 自带的复制
- // outChannel.transferFrom(inChannel, 0, inChannel.size());
- outChannel.close();
- inChannel.close();
- fin.close();
- fout.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- java nio 复制文件功能
- Java NIO复制文件功能
- java NIO复制文件
- JAVA功能代码《12》----使用NIO快速复制文件
- 【Java IO】NIO复制文件
- java文件普通复制和NIO复制
- JAVA IO和NIO复制文件
- java实现文件复制功能
- java实现文件复制功能
- 利用Java NIO 实现文件监控功能。
- 使用NIO复制文件
- NIO 复制文件。
- NIO复制文件
- Java NIO文件复制以及与传统IO效率对比
- java NIO实现的文件复制(包括文件与文件夹复制) NIO读取数据与写数据
- JAVA 实现文件的“复制”功能
- 用java实现文件的复制功能
- java io 复制文件与 nio 复制文件的效率问题
- APACHE关于日志格式的配置:
- 微软企业库5.0学习笔记(四)配置企业库
- {UVA} UVA-414 解题报告
- PSD的网页模版PS切图到网页生成全攻略
- 我个人比较受用的一些习惯-转载
- java nio 复制文件功能
- 帳票の作成
- 另类的seo技巧。
- IIS中FTP虚拟目录的妙用
- 存储过程调用存储过程地疑难
- 使用Carbide c++ 2.0在S60 Nokia 6120C上搭建真机debug调试环境的要点 收藏
- 月光论坛(williamlong)被CCTV焦点访谈暗杀的背后
- matlab 引擎实现混合编程问题。
- SWT 打包