java中IO流的学习笔记(2、复制数据)
来源:互联网 发布:网络用语八组鹅 编辑:程序博客网 时间:2024/04/28 22:11
Java IO流中文件的复制:
思想:
1、源文件
/Users/liujing/Desktop/123.mp3
2、目标文件
/Users/liujing/Desktop/321.mp3
确定源文件与目标文件之后我们就可以使用字节流的方式对目标文件进行复制。代码如下:
package com.lj.iodemo;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class IODemo3 { public static void main(String[] args) throws IOException { // 封装数据源 // FileInputStream fileInputStream = new // FileInputStream("/Users/liujing/Desktop/a.rtf"); FileInputStream fileInputStream = new FileInputStream("/Users/liujing/Desktop/123.mp3"); // 封装目的地 // FileOutputStream fileOutputStream = new // FileOutputStream("/Users/liujing/Desktop/b.rtf"); FileOutputStream fileOutputStream = new FileOutputStream("/Users/liujing/Desktop/321.mp3"); // 一个字节一个的复制 int by = 0; while ((by = fileInputStream.read()) != -1) { fileOutputStream.write(by); } fileInputStream.close(); fileOutputStream.close(); }}
但是以上代码对于稍大的文件复制效率很低所以我们可以采取以下办法:
我们可以使用java带缓冲区的高效类
写数据:
BufferedOutputStream
在这里我们自定义一个数组java在读取的时候一次按1024字节进行读取,可以提高程序效率。代码如下:
package com.lj.iodemo;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;public class IODemo5 { public static void main(String[] args) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt")); byte[] bys = new byte[1024]; int by = 0; while ((by = bis.read(bys)) != -1) { System.out.println(new String(bys, 0, by)); } bis.close(); }}
读数据:
BufferedIntputStream
实现代码如下:
package com.lj.iodemo;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.IOException;public class IODemo4 { public static void main(String[] args) throws IOException {// FileOutputStream fis = new FileOutputStream("bos.txt");// BufferedOutputStream bos = new BufferedOutputStream(fis); /* * 因为BufferedOutputStream的构造方法需要的是一个OutputStream对象,而OutputStream又是一个 * 抽象类所以这里我们使用他的子类FileOutputStream将其对象传入。以上方法还可以简写为代码如下: */ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt")); int by = 0; bos.write("hello".getBytes()); bos.close(); }}
下面我们用这四中方式对文件进行复制,并测试消耗时长:
package com.lj.iodemo;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/* * 字节流四种方式复制文件: * 基本字节流一次读写一个字节 * 基本字节流一次读写一个字节数组 * 高效字节流一次读写一个字节 * 高效字节流一次读写一个字节数组 */public class IODemo6 { public static void main(String[] args) throws IOException { method1("/Volumes/移动硬盘/IO复制测试/第一行代码.pdf", "/Volumes/移动硬盘/IO复制测试/第一行代码a.pdf"); method2("/Volumes/移动硬盘/IO复制测试/第一行代码.pdf", "/Volumes/移动硬盘/IO复制测试/第一行代码b.pdf"); method3("/Volumes/移动硬盘/IO复制测试/第一行代码.pdf", "/Volumes/移动硬盘/IO复制测试/第一行代码c.pdf"); method4("/Volumes/移动硬盘/IO复制测试/第一行代码.pdf", "/Volumes/移动硬盘/IO复制测试/第一行代码d.pdf"); } public static void method1(String srcfile, String destfile) throws IOException { long start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(srcfile); FileOutputStream fos = new FileOutputStream(destfile); int by = 0; while ((by = fis.read()) != -1) { fos.write(by); } fis.close(); fos.close(); long end = System.currentTimeMillis(); long time = end - start; System.out.println("基本字节流一次读写一个字节耗时 " + time + "毫秒"); } public static void method2(String srcfile, String destfile) throws IOException { long start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(srcfile); FileOutputStream fos = new FileOutputStream(destfile); int by = 0; byte[] bys = new byte[1024]; while ((by = fis.read(bys)) != -1) { fos.write(bys,0,by); } fis.close(); fos.close(); long end = System.currentTimeMillis(); long time = end - start; System.out.println("基本字节流一次读写一个字节数组 " + time + "毫秒"); } public static void method3(String srcfile, String destfile) throws IOException { long start = System.currentTimeMillis(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcfile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destfile)); int by = 0; while ((by = bis.read()) != -1) { bos.write(by); } bis.close(); bos.close(); long end = System.currentTimeMillis(); long time = end - start; System.out.println("高效字节流一次读写一个字节 " + time + "毫秒"); } public static void method4(String srcfile, String destfile) throws IOException { long start = System.currentTimeMillis(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcfile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destfile)); int by = 0; byte[] bys = new byte[1024]; while ((by = bis.read(bys)) != -1) { bos.write(bys,0,by); } bis.close(); bos.close(); long end = System.currentTimeMillis(); long time = end - start; System.out.println("高效字节流一次读写一个字节数组 " + time + "毫秒"); }}
最后得到时间为,可以看到不同方式见效率存在很大区别
0 0
- java中IO流的学习笔记(2、复制数据)
- java中IO流的学习笔记(1、字节流写入数据)
- java基础-用IO演示文件的复制过程(学习笔记)
- java IO流中File类的学习笔记
- Java语言中IO流的操作规律学习笔记
- 黑马程序员-学习笔记2-IO流之文件复制
- java中IO流的文件简单的复制
- java中IO流的学习笔记(3、转换流的写入和读取)
- java中通过IO流实现文件的复制
- Java IO 实现文件复制 -Java 学习笔记 (25)
- 【我的Java笔记】IO流_使用IO流进行文件的复制和粘贴
- Java学习笔记--IO流的操作
- JAVA学习第四十八课 — IO流(二):文件的复制 & 缓冲区1
- Java IO的个人学习笔记(2)
- java中最简单的文件复制,读写(IO流操作)
- JAVA-IO流中缓存区的使用(BufferedReader和BufferedWriter)以及复制内容
- 【我的Java笔记】IO流_输出流中给文本文件追加数据的方法
- Java学习笔记之IO(六):字节流对中文数据的读写
- Mac OS 下监听/拦截 TCP/UDP 连接
- 2015年读过的书
- poj--1273(最大流基础题)
- BZOJ 1854[Scoi2010]游戏
- poj3281 最大流
- java中IO流的学习笔记(2、复制数据)
- Spring 4.1.7 MVC @RequestPart 问题
- android Notification使用总结
- Android系统学习笔记
- ubuntu使用技巧——windows和ubuntu双系统的注意问题
- centos上如何yum安装git
- vector 常用函数
- Python操作SQLite/MySQL/LMDB/LevelDB
- VVDocumenter Xcode 插件失效问题