采纳FileInputStream批量文件复制 和 BufferedInputStream缓冲流的文件复制

// RandomAccessFile 读写操作

public static void ranfile(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, "r");

RandomAccessFile rw = new RandomAccessFile(copyFile, "rw");

Long start = System.nanoTime();

String str = null;

while((str = r.readLine()) != null) {

rw.writeBytes(str);

}

Long end = System.nanoTime();

// 562885

System.out.println("ranfile复制文件耗时:" + (end - start));

r.close();

rw.close();

}

// RandomAccessFile 读写操作

public static void ranfileOne(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, "r");

RandomAccessFile rw = new RandomAccessFile(copyFile, "rw");

Long start = System.nanoTime();

// 文件复制没有完成

int b = r.read();

rw.write(b);

Long end = System.nanoTime();

// 408812

System.out.println("ranfileOne复制文件耗时:" + (end - start));

r.close();

rw.close();

}

// RandomAccessFile 读写操作

public static void ranfiletwo(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, "r");

RandomAccessFile rw = new RandomAccessFile(copyFile, "rw");

Long start = System.nanoTime();

int t = -1;

while ((t = r.read()) != -1) {

rw.write(t);

}

Long end = System.nanoTime();

// 594078

System.out.println("ranfiletwo复制文件耗时:" + (end - start));

r.close();

rw.close();

}

// RandomAccessFile 读写操作 批量

public static void ranfileBatchThree(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, "r");

RandomAccessFile rw = new RandomAccessFile(copyFile, "rw");

Long start = System.nanoTime();

byte[] bs = new byte[(int) r.length()];

int t = -1;

while ((t = r.read(bs)) != -1) {

rw.write(bs);

}

Long end = System.nanoTime();

// 427245

System.out.println("ranfileBatchThree复制文件耗时:" + (end - start));

r.close();

rw.close();

}

// RandomAccessFile 读写操作 批量

public static void ranfileBatch(File file, File copyFile) throws IOException {

RandomAccessFile r = new RandomAccessFile(file, "r");

RandomAccessFile rw = new RandomAccessFile(copyFile, "rw");

Long start = System.nanoTime();

byte[] bs = new byte[(int) r.length()];

int t = -1;

while ((t = r.read(bs, 0, bs.length)) != -1) {

rw.write(bs, 0, bs.length);

}

Long end = System.nanoTime();

// 427245

System.out.println("ranfileBatch复制文件耗时:" + (end - start));

r.close();

rw.close();

}

public static void fileStream(File file, File copyFile) throws IOException {

// 使用FileInputStream 和 FileOutputStream 一个字节一个字节读写操作

FileInputStream in = new FileInputStream(file);

FileOutputStream out = new FileOutputStream(copyFile);

Long start = System.nanoTime();

int t = -1;

while ((t = in.read()) != -1) {

out.write(t);

}

Long end = System.nanoTime();

// 871503

System.out.println("fileStream复制文件耗时:" + (end - start));

in.close();

out.close();

}

// 使用FileInputStream 和 FileOutputStream 批量操作

public static void fileStreamBatchOne(File file, File copyFile) throws IOException {

FileInputStream in = new FileInputStream(file);

FileOutputStream out = new FileOutputStream(copyFile);

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = in.read(bs)) != -1) {

out.write(bs);

}

Long end = System.nanoTime();

// 609675

System.out.println("fileStreamBatchOne复制文件耗时:" + (end - start));

in.close();

out.close();

}

// 使用FileInputStream 和 FileOutputStream 批量操作

public static void fileStreamBatch(File file, File copyFile) throws IOException {

FileInputStream in = new FileInputStream(file);

FileOutputStream out = new FileOutputStream(copyFile);

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = in.read(bs,0,bs.length)) != -1) {

out.write(bs,0,bs.length);

}

Long end = System.nanoTime();

// 609675

System.out.println("fileStreamBatch复制文件耗时:" + (end - start));

in.close();

out.close();

}

// DataInputStream 和 DataOutputStream 进行读写操作

public static void dataFile(File file, File copyFile) throws IOException {

DataInputStream dis = new DataInputStream(new FileInputStream(file));

DataOutputStream dos = new DataOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

while ((t = dis.read()) != -1) {

dos.write(t);

}

Long end = System.nanoTime();

// 691910

System.out.println("dataFile复制文件耗时:" + (end - start));

dis.close();

dos.close();

}

// DataInputStream 和 DataOutputStream 进行批量读写操作

public static void dataFileBatchOne(File file, File copyFile) throws IOException {

DataInputStream dis = new DataInputStream(new FileInputStream(file));

DataOutputStream dos = new DataOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = dis.read(bs)) != -1) {

dos.write(bs);

}

Long end = System.nanoTime();

// 692854

System.out.println("dataFileBatchOne复制文件耗时:" + (end - start));

dis.close();

dos.close();

}

// DataInputStream 和 DataOutputStream 进行批量读写操作

public static void dataFileBatch(File file, File copyFile) throws IOException {

DataInputStream dis = new DataInputStream(new FileInputStream(file));

DataOutputStream dos = new DataOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = dis.read(bs,0,bs.length)) != -1) {

dos.write(bs,0,bs.length);

}

Long end = System.nanoTime();

// 692854

System.out.println("dataFileBatch复制文件耗时:" + (end - start));

dis.close();

dos.close();

}

// BufferedInputStream 和 BufferedOutputStream 进行复制文件操作

public static void bufferedFile(File file, File copyFile) throws IOException {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

while ((t = bis.read()) != -1) {

bos.write(t);

bos.flush();

}

Long end = System.nanoTime();

// 1443369

System.out.println("bufferedFile复制文件耗时:" + (end - start));

bis.close();

bos.close();

}

// BufferedInputStream 和 BufferedOutputStream 进行批量复制文件操作

public static void bufferedFileBatchOne(File file, File copyFile) throws IOException {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = bis.read(bs)) != -1) {

bos.write(bs);

bos.flush();

}

Long end = System.nanoTime();

// 589352

System.out.println("bufferedFileBatch复制文件耗时:" + (end - start));

bis.close();

bos.close();

}

// BufferedInputStream 和 BufferedOutputStream 进行批量复制文件操作

public static void bufferedFileBatch(File file, File copyFile) throws IOException {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyFile));

Long start = System.nanoTime();

int t = -1;

byte[] bs = new byte[(int) file.length()];

while ((t = bis.read(bs,0,bs.length)) != -1) {

bos.write(bs,0,bs.length);

bos.flush();

}

Long end = System.nanoTime();

// 589352

System.out.println("bufferedFileBatch复制文件耗时:" + (end - start));

bis.close();

bos.close();

}

public static void main(String[] args) throws IOException {

String oldPath = "E:an.txt";

String newPath = "E:one4.txt";

File oldFile = new File(oldPath);

File copyFile = new File(newPath);

// 视频不可以复制

IOCompare.ranfile(oldFile,copyFile);

// 视频不可以复制

// IOCompare.ranfileOne(oldFile, copyFile);

// IOCompare.ranfiletwo(oldFile, copyFile);

// IOCompare.ranfileBatchThree(oldFile, copyFile);

// 3697745-mp4,2608839-wmv,2592769-mp3,

// IOCompare.ranfileBatch(oldFile, copyFile);

// IOCompare.fileStream(oldFile, copyFile);

// IOCompare.fileStreamBatchOne(oldFile, copyFile);

// 5078728

// IOCompare.fileStreamBatch(oldFile, copyFile);

// IOCompare.dataFile(oldFile, copyFile);

// IOCompare.dataFileBatchOne(oldFile, copyFile);

// 4670388,5995130,2657991

// IOCompare.dataFileBatch(oldFile, copyFile);

// IOCompare.bufferedFile(oldFile, copyFile);

// IOCompare.bufferedFileBatchOne(oldFile, copyFile);

// 3022850,3602277,2991658

// IOCompare.bufferedFileBatch(oldFile, copyFile);

}

一、字符流

(1)Reader是所有字符输入流的父类

(2)Writer是所有字符输出流的父类

(3)字符流以字符(char)为单位进行读写

(4)编码和解码

InputStreamReader,字符输入流,字节数据转字符数据(编码)

OutputStreamWriter,字节输出流,将字符数据转字节数据(解码)

(5)编码格式

可以指定编码格式的:OutputStreamWriter,InputStreamReader

二、字节流

(1)InputStream是所有字节输入流的父类

(2)OutputStream是所有字节流输出流的父类

(3)以字节为单位进行数据的读写

(4)缓冲流中的 void flush()方法,是将缓冲区的数据强制写出

(5)对象流

对象 ---> 字节序列 : 对象序列化

字节序列 ---> 对象 : 对象反序列化

cfile file 读写最大文件_java的IO字节流复制文件对比(2)相关推荐

  1. java缓冲流 复制文件_java使用缓冲流复制文件的方法

    本文实例为大家分享了java使用缓冲流复制文件的具体代码,供大家参考,具体内容如下 [1] 程序设计 /*------------------------------- 1.缓冲流是一种处理流,用来加 ...

  2. IO流---复制文件内容

    IO流:用来进行设备间的数据传输问题. IO流根据流向可以分为输入流和输出流,其中输出流,从一个设备向一个设备传数据,自然是先读取数据,所以输出流既是读取,同理输入流既是写入. IO流又根据数据类型分 ...

  3. java io流复制文件简单实例

    java io流复制文件简单实例 实例1: package com.io;import java.io.BufferedInputStream; import java.io.BufferedOutp ...

  4. 字节流复制文件 java

    字节流复制文件 java #mermaid-svg-5dltjDbFFRmQ4XcJ .label{font-family:'trebuchet ms', verdana, arial;font-fa ...

  5. file数组 删除文件_java编程IO基础之一:File类

    在整个java.io包中,File类是唯一的一个与文件本身操作有关的类,既可以执行文件的创建.删除.重命名,取得文件大小和修改日期. File的构造函数: public File(String pat ...

  6. java 性能测试文档模板_Java中IO字节流基本操作(复制文件)并测试性能

    此次案例将以复制文件的形式来演示IO字节流的基本操作,复制一个mp3文件,文件信息如下图: main方法测试 public static void main(String[] args) throws ...

  7. c++语言文件流,C++ IO类、文件输入输出、string流详细讲解

    新的C++标准中有三分之二的内容都是描述标准库.接下来重点学习其中几种核心库设施,这些是应该熟练掌握的. 标准库的核心是很多容器类(顺序容器和关联容器等)和一簇泛型算法(该类算法通常在顺序容器一定范围 ...

  8. java追加txt文件_java怎么追加写入txt文件

    java中,对文件进行追加内容操作的三种方法! import java.io.BufferedWriter;import java.io.FileOutputStream;import java.io ...

  9. java 导入导出txt文件_Java读取和写入txt文件

    1 问题描述 对于java的读取和写入txt一直心存疑惑,随着知识的积累,又重新进行学习,对java的文件读写理解更加深刻,在这里将自己的小小经验总结分享给大家.下面是大家了解java流的一个基本框架 ...

最新文章

  1. python语言入门m-Python学习基础篇 -1
  2. Asp将查询结果导出到excel里
  3. Redhat下XFS的安装
  4. 集成电路设计软件Tanner 16.30安装教程(附安装包)
  5. 《Ansible权威指南 》一第2章 Ansible基础元素介绍
  6. Python 链表内取随机数(list取随机数)
  7. boost采取什么驱动电路_当我们只是采取积极的意愿时会发生什么?
  8. C语言怎么实现熊猫上香中的系统错误提示,熊猫烧香的病毒是用什么程序语言编写的 原理是什么...
  9. win7下ApmServ启动失败问题
  10. RGB-D深度相机原理
  11. 火狐浏览器批量保存网页图片
  12. 【MATLAB深度学习工具箱】学习笔记--体脂估计算例再分析:拟合神经网络fitnet里面的数据结构】
  13. ios打没有签名的ipa包
  14. python弹性碰撞次数圆周率_碰撞出来的圆周率(一)
  15. 实验三:CART分类决策树python实现(两个测试集)(一)|机器学习
  16. 鼠标测试cps软件,人最多鼠标cps是多少?
  17. 微信 商店服务器,对比saas小程序,云开发,微信小商店
  18. MATLAB—经纬度坐标转换为平面坐标
  19. 【挑战30天掌握】算法与数据结构!!!
  20. MBA案例分析(人员培训二)

热门文章

  1. 实验2-3-7 求平方与倒数序列的部分和 (C语言)
  2. Open3d之颜色映射优化
  3. c#与access建立连接用作登录_SQLServer成功与服务器建立连接,但在登录中发生错误
  4. 简单的Verilog测试模板结构
  5. 对于Linux安装mysql5.7版本出现的问题
  6. web开发中的跨域问题
  7. 时序分析基本概念介绍<Slew/Transition>
  8. 计算机电池功能,蓄电池检测仪的主要功能都有哪些
  9. oracle x kglob,x$kglob x$kgllk x$kglpn
  10. 浅谈矩阵分解在推荐系统中的应用