您现在的位置是:主页 > news > 成都网站公司/深圳互联网营销

成都网站公司/深圳互联网营销

admin2025/5/20 18:17:38news

简介成都网站公司,深圳互联网营销,淘宝页面设计的网站,云主机和云服务器有什么区别首先模拟实现一个fileUtils工具类。里面的copy方法用于拷贝文件数据,参数是两个文件流对象分别为输入和输出流,即拷贝的源和目的。 对于以下实现的copy, copy2, copy3三个copy方法中 第一个copy只需要传要拷贝的文件的源和目的文件的字符串调用copy函数…

成都网站公司,深圳互联网营销,淘宝页面设计的网站,云主机和云服务器有什么区别首先模拟实现一个fileUtils工具类。里面的copy方法用于拷贝文件数据,参数是两个文件流对象分别为输入和输出流,即拷贝的源和目的。 对于以下实现的copy, copy2, copy3三个copy方法中 第一个copy只需要传要拷贝的文件的源和目的文件的字符串调用copy函数…

首先模拟实现一个fileUtils工具类。里面的copy方法用于拷贝文件数据,参数是两个文件流对象分别为输入和输出流,即拷贝的源和目的。

对于以下实现的copy, copy2, copy3三个copy方法中

第一个copy只需要传要拷贝的文件的源和目的文件的字符串调用copy函数就能完成拷贝

public static void copy(String srcPath, String destPath) {//创建源File srcFile = new File(srcPath);File destFile = new File(destPath);//选择流InputStream inputStream = null;OutputStream outputStream = null;try {inputStream = new FileInputStream(srcFile);outputStream = new FileOutputStream(destFile);//分段读取byte[] flush = new byte[1024];int len = -1;while((len = inputStream.read(flush)) != -1) {outputStream.write(flush, 0, len);}}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally {try {if(null != inputStream)inputStream.close();}catch (IOException e) {e.printStackTrace();}try {if(null != outputStream)outputStream.close();}catch (IOException e) {e.printStackTrace();}}}

第二个copy2函数相较于第一个copy将参数设置成要拷贝的文件的源和目的文件的输入输出流1对象

//封装对接输入输出流public static void copy2(InputStream inputStream, OutputStream outputStream) {try {//分段读取byte[] flush = new byte[1024];int len = -1;while((len = inputStream.read(flush)) != -1) {outputStream.write(flush, 0, len);}}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally {try {if(null != inputStream)inputStream.close();}catch (IOException e) {e.printStackTrace();}try {if(null != outputStream)outputStream.close();}catch (IOException e) {e.printStackTrace();}}}

第三个copy3函数相较于第二个copy2函数对关闭文件流资源进行了封装,他们实现的功能都是相同的。

//在copy2的基础上对释放资源的操作封装成函数public static void copy3(InputStream inputStream, OutputStream outputStream) {try {//分段读取byte[] flush = new byte[1024];int len = -1;while((len = inputStream.read(flush)) != -1) {outputStream.write(flush, 0, len);}}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally {//释放资源close(inputStream, outputStream);}}public static void close(InputStream inputStream, OutputStream outputStream) {try {if(null != inputStream)inputStream.close();}catch (IOException e) {e.printStackTrace();}try {if(null != outputStream)outputStream.close();}catch (IOException e) {e.printStackTrace();}}
测试文本到文本,图片到图片, 文本到字节流再到图片的拷贝
package cn.yzy.io;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class fileUtils {public static void main(String[] args) {/** 由于FileInputStream和FileOutputStream构造器都有接收String类型* 并new为File类型实例并创建FileInputStream和FileOutputStream实例的功能*///测试文件到文件拷贝try {InputStream inputStream = new FileInputStream("src.txt");OutputStream outputStream = new FileOutputStream("dest-copy.txt");copy2(inputStream, outputStream);}catch (IOException e) {/** IOException是FileNotFoundException的基类,* 除去之前的代码以后只用IOException捕捉异常就够了*/e.printStackTrace();}//测试图片拷贝try {InputStream inputStream = new FileInputStream("whale.jpg");OutputStream outputStream = new FileOutputStream("whale-fileUtils-copy.jpg");copy2(inputStream, outputStream);}catch (IOException e) {e.printStackTrace();}//测试图片到字节流byte[] datas = null;try {InputStream inputStream = new FileInputStream("whale.jpg");ByteArrayOutputStream outputStream = new ByteArrayOutputStream();copy2(inputStream, outputStream);datas = outputStream.toByteArray();System.out.println(datas.length);}catch (IOException e) {e.printStackTrace();}//测试字节流到图片try {ByteArrayInputStream inputStream = new ByteArrayInputStream(datas);OutputStream outputStream = new FileOutputStream("ByteArray2File.jpg");copy2(inputStream, outputStream);}catch (IOException e) {e.printStackTrace();}}//基于上一个对接流博客写的copy方法,后面copy2是对其做的改进和封装public static void copy(String srcPath, String destPath) {//创建源File srcFile = new File(srcPath);File destFile = new File(destPath);//选择流InputStream inputStream = null;OutputStream outputStream = null;try {inputStream = new FileInputStream(srcFile);outputStream = new FileOutputStream(destFile);//分段读取byte[] flush = new byte[1024];int len = -1;while((len = inputStream.read(flush)) != -1) {outputStream.write(flush, 0, len);}}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally {try {if(null != inputStream)inputStream.close();}catch (IOException e) {e.printStackTrace();}try {if(null != outputStream)outputStream.close();}catch (IOException e) {e.printStackTrace();}}}//封装对接输入输出流public static void copy2(InputStream inputStream, OutputStream outputStream) {try {//分段读取byte[] flush = new byte[1024];int len = -1;while((len = inputStream.read(flush)) != -1) {outputStream.write(flush, 0, len);}}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally {try {if(null != inputStream)inputStream.close();}catch (IOException e) {e.printStackTrace();}try {if(null != outputStream)outputStream.close();}catch (IOException e) {e.printStackTrace();}}}//在copy2的基础上对释放资源的操作封装成函数public static void copy3(InputStream inputStream, OutputStream outputStream) {try {//分段读取byte[] flush = new byte[1024];int len = -1;while((len = inputStream.read(flush)) != -1) {outputStream.write(flush, 0, len);}}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally {//释放资源close(inputStream, outputStream);}}public static void close(InputStream inputStream, OutputStream outputStream) {try {if(null != inputStream)inputStream.close();}catch (IOException e) {e.printStackTrace();}try {if(null != outputStream)outputStream.close();}catch (IOException e) {e.printStackTrace();}}}

测试文件拷贝,结果发现数据文件2拷贝都正常

文本到文本
图片到图片
文本到字节流再到图片

对于释放文件流资源而封装的close函数我们又可以做如下改进:

借助Closeable接口以及可变参数进行封装
//利用Closeable接口和可变参数进一步优化closepublic static void close(Closeable ...ios) {for(Closeable io : ios) {try {if(null != io) {io.close();}}catch (IOException e) {e.printStackTrace();}}}

jdk1.7之后对释放文件资源增加了try…with…resources语法,这样我们就不用手动对文件流资源释放了,于是我们对copy3和copy函数同时做了改进,以便只管了解try-with-resource语法

借助try…with…resources语法,我们对copy3代码进行改进
//jdk1.7之后对释放文件资源增加了try...with...resources语法,我们对copy3代码进行改进如下:public static void copy4(InputStream inputStream, OutputStream outputStream) {try(inputStream; outputStream) {//分段读取byte[] flush = new byte[1024];int len = -1;while((len = inputStream.read(flush)) != -1) {outputStream.write(flush, 0, len);}}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}}
继续通过try…with…resources语法,我们继续对copy的代码进行改进

对copy的代码改造,只需要将文件流对象的声明放入try…with…resources就行

public static void copy5(String srcPath, String destPath) {//创建源File srcFile = new File(srcPath);File destFile = new File(destPath);try(InputStream inputStream = new FileInputStream(srcFile);OutputStream outputStream = new FileOutputStream(destFile)) {//分段读取byte[] flush = new byte[1024];int len = -1;while((len = inputStream.read(flush)) != -1) {outputStream.write(flush, 0, len);}}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}}

  1. InputStream和OutputStream ↩︎

  2. 文本到文本,图片到图片, 文本到字节流再到图片 ↩︎