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 + "毫秒");
}
}
最后得到时间为,可以看到不同方式见效率存在很大区别