package 学习资料.IO流;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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 _5_1_文件拷贝 {public static void main(String[] args) {byte[] path = FiletoByte(");BytetoFile(",path);}public static byte[] FiletoByte(String filepath) {File src = new File(filepath); //建立文件对象InputStream is = null; //字节输入流ByteArrayOutputStream baos = null; //字节操作流try {is = new FileInputStream(src); //打开文件对象baos = new ByteArrayOutputStream(); //获得操作对象byte[] flush = new byte[1024*10]; //字节数组缓存int len = -1; //单次长度while((len = is.read(flush)) != -1) //缓存获得文件信息baos.write(flush,0,len); //将缓存信息写入操作对象baos.flush(); //刷新操作对象ByteArray(); //将操作对象存储的字节信息返回} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {if(is != null) is.close();} catch (IOException e) {e.printStackTrace();}}return null;}public static void BytetoFile(String filePath,byte[] src) {File dest = new File(filePath); //建立文件对象InputStream is = null; //输入字节流OutputStream os = null; //输出字节流try {is = new ByteArrayInputStream(src); //将src写入对象os = new FileOutputStream(dest); //打开文件对象byte[] flush = new byte[5]; //字节输出缓存int len = -1; //单次长度while((len = is.read(flush)) != -1) //从将输入流写入缓存os.write(flush,0,len); //将缓存写进文件os.flush(); //刷新输出流} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {if( os != null) os.close();} catch (IOException e) {e.printStackTrace();}}}
}
package 学习资料.IO流;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
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 _5_1_FileUtiles_文件拷贝 {public static void main(String[] args) {//文件-->>文件try {//所有文件的复制都可以这样使用,包括但不限于png,jpg,gif,txt,class等InputStream iStream = new FileInputStream(");OutputStream oStream = new FileOutputStream("");BytetoFile(iStream,oStream);// close(iStream,oStream);} catch (IOException e) {e.printStackTrace();}//文件-->>字节数组byte[] information = null;try {InputStream iStream = new FileInputStream("a_copy.png");ByteArrayOutputStream oStream = new ByteArrayOutputStream();BytetoFile(iStream,oStream);information = ByteArray();// close(iStream,oStream);} catch (IOException e) {e.printStackTrace();}//字节数组-->>文件try {//所有文件的复制都可以这样使用,包括但不限于png,jpg,gif,txt,class等InputStream iStream = new ByteArrayInputStream(information);OutputStream oStream = new FileOutputStream("copy.png");BytetoFile(iStream,oStream);// close(iStream,oStream);} catch (IOException e) {e.printStackTrace();}/*** 之前想过明明可以直接将文件复制为什么还需要转换成字节数组这个中间变量呢?* 转换成字节数组的优点在于不只可以在本地进行传输(复制)文件了,因为网络只能接收字节流,所以可以将文件转化成字节数组传输到其他主机上* 其次,字节数组可以进行加密,可以保证数据在网络传输上的安全性* 还有就是可以将单个文件拆分成多个小文件,比如别人想爬取一个视频但是只能爬取到一个个小视频无法整合,整合手段对方爬取不到从而达到保护文件的目的*/}public static void BytetoFile(InputStream is,OutputStream os) {try(InputStream is1 =new BufferedInputStream(is); //JDK1.8中的try-with-resources语法糖,这样写就可以省掉close部分OutputStream os1 =new BufferedOutputStream(os)) { //可以省去close 的封装操作1//Buffered可以提高效率操作方式没变(类似与装饰模式)byte[] flush = new byte[1024]; //字节输出缓存int len = -1; //单次长度while((len = ad(flush)) != -1) //从将输入流写入缓存os1.write(flush,0,len); //将缓存写进文件os1.flush(); //刷新输出流} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void close(InputStream is,OutputStream os) {try {if( os != null) os.close();} catch (IOException e) {e.printStackTrace();}try {if(is !=null)is.close();} catch (IOException e) {e.printStackTrace();}}//接口是一个特殊的类,也可以作为参数// ↓可变参数public static void ios) {for(Closeable io:ios) {try {if( io != null) io.close();} catch (IOException e) {e.printStackTrace();}}}
}
本文发布于:2024-01-29 19:00:18,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170652602217582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |