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

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

9cea8e94e4b74900f27da3f55e5a030f.png

// 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);

}

b243bf94b777b89108ebd1aebe58d9f8.png

一、字符流

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

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

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

(4)编码和解码

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

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

(5)编码格式

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

二、字节流

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

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

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

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

(5)对象流

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

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

27ae3ea8c8a60a74fa7ecdafb4db8e61.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值