JAVA中IO流部分复制文件常用的四中方法
2015-10-24 21:36
555 查看
IO流中复制文件的方法多种多样,今天主要讲解一下一下四种常用的复制文件的方法,每个方法实现复制文件的效率都有所不同,以后在做项目中大家可以根据实际情况来决定使用哪种复制方式来复制文件:
1. 字节流读写单个字节
2. 字节流读写字节数组
3. 字节流缓冲区读写单个字节
4. 字节流缓冲区读写字节数组
第一种:用字节流读取单个文件的方式
public static void copy_1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(“c:\tools.jar”);
fos = new FileOutputStream(“d:\tools.jar”);
int len = 0 ;
while((len = fis.read())!=-1){
fos.write(len);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(“复制失败”);
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}
}
}
}
第二种:字节流读写字节数组
public static void copy_2(){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(“c:\tools.jar”);
fos = new FileOutputStream(“d:\tools.jar”);
byte[] bytes = new byte[1024];
int len = 0 ;
while((len = fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(“复制失败”);
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}
}
}
}
第三种:字节流缓冲区读写单个字节
public static void copy_3(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(“c:\tools.jar”));
bos = new BufferedOutputStream(new FileOutputStream(“d:\tools.jar”));
int len = 0 ;
while((len = bis.read())!=-1){
bos.write(len);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(“复制失败”);
}finally{
try{
if(bos!=null)
bos.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}finally{
try{
if(bis!=null)
bis.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}
}
}
}
第四种:字节流缓冲区读写字节数组
public static void copy_4(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(“c:\tools.jar”));
bos = new BufferedOutputStream(new FileOutputStream(“d:\tools.jar”));
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(“复制失败”);
}finally{
try{
if(bos!=null)
bos.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}finally{
try{
if(bis!=null)
bis.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}
}
}
}
以上四种方法,经实际测试,第二和第四中方法复制文件的速度要明显快于其他两种,应为它是以数组为单位进行复制文件的,而其他两种都是以单个字节的方式来进行复制的,所以会慢一些,以后玩文件复减一还是用第二和第四种文件复制的方式吧.
1. 字节流读写单个字节
2. 字节流读写字节数组
3. 字节流缓冲区读写单个字节
4. 字节流缓冲区读写字节数组
第一种:用字节流读取单个文件的方式
public static void copy_1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(“c:\tools.jar”);
fos = new FileOutputStream(“d:\tools.jar”);
int len = 0 ;
while((len = fis.read())!=-1){
fos.write(len);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(“复制失败”);
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}
}
}
}
第二种:字节流读写字节数组
public static void copy_2(){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(“c:\tools.jar”);
fos = new FileOutputStream(“d:\tools.jar”);
byte[] bytes = new byte[1024];
int len = 0 ;
while((len = fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(“复制失败”);
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}
}
}
}
第三种:字节流缓冲区读写单个字节
public static void copy_3(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(“c:\tools.jar”));
bos = new BufferedOutputStream(new FileOutputStream(“d:\tools.jar”));
int len = 0 ;
while((len = bis.read())!=-1){
bos.write(len);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(“复制失败”);
}finally{
try{
if(bos!=null)
bos.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}finally{
try{
if(bis!=null)
bis.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}
}
}
}
第四种:字节流缓冲区读写字节数组
public static void copy_4(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(“c:\tools.jar”));
bos = new BufferedOutputStream(new FileOutputStream(“d:\tools.jar”));
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(“复制失败”);
}finally{
try{
if(bos!=null)
bos.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}finally{
try{
if(bis!=null)
bis.close();
}catch(IOException ex){
throw new RuntimeException(“资源关闭失败”);
}
}
}
}
以上四种方法,经实际测试,第二和第四中方法复制文件的速度要明显快于其他两种,应为它是以数组为单位进行复制文件的,而其他两种都是以单个字节的方式来进行复制的,所以会慢一些,以后玩文件复减一还是用第二和第四种文件复制的方式吧.
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统