当前位置: 首页 > news >正文

Java高级_Day11(复制文件,字节缓冲流)

Java高级_Day11(复制文件,字节缓冲流)

复制文件

/*将D:\\IO\os\\窗里窗外.txt文档复制到当前module中*/
public class FileCopy {public static void main(String[] args) throws IOException {// 实现读数据FileInputStream  fis = new FileInputStream("D:\\IO\\os\\窗里窗外.txt");// 实现写数据FileOutputStream  fos = new FileOutputStream("day_11_IODemo\\copy\\窗里窗外.txt");// 使用输入流来读数据 每次读取一个字节 读完之后  立即使用输出流将数据写出到目标文件int len;long begin = System.currentTimeMillis();// 获取开始 复制的时间while((len = fis.read()) != -1){fos.write(len);}long end = System.currentTimeMillis();System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");//复制文件完成,共耗时:2189毫秒// 释放资源fos.close();fis.close();}
}

使用字节流读取数据(每次读取一个字节数组)

public class FileReadByteArray {public static void main(String[] args) throws IOException {FileInputStream  fis = new FileInputStream("D:\\IO\\os\\fos.txt");//  int read(byte[] b)byte[] buff = new byte[10];int len;while( (len =fis.read(buff)) != -1){//每次读取10个字节  在去输出// 将读取到的字节数组转换为字符串 输出System.out.println(new String(buff));}fis.close();}

在这里插入图片描述

0123456789
hello\r\nwor
ld\r\nhello\r
\nworldllo\r

解决方法:
String(byte[] bytes, int offset, int length)
通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。

public class FileReadByteArray {public static void main(String[] args) throws IOException {FileInputStream  fis = new FileInputStream("D:\\IO\\os\\fos.txt");//  int read(byte[] b)byte[] buff = new byte[10];int len;while( (len =fis.read(buff)) != -1){//每次读取10个字节  在去输出// 将读取到的字节数组转换为字符串 输出转换的时候 读几个就转换几个System.out.print(new String(buff,0,len));//}fis.close();}
}

使用每次读取一个字节数组的方式 实现文件的复制:

/*** @author Mr.Hu* @create 2021/01/13**     将D:\\IO\os\\窗里窗外.txt文档复制到当前module中  采用读取一个字节数组的方式**/
public class FileCopyByByteArray {public static void main(String[] args) throws IOException {FileInputStream  fis= new FileInputStream("d:\\IO\\os\\窗里窗外.txt");FileOutputStream fos = new FileOutputStream("day_11_IODemo\\copy\\copy.txt");byte[] buff = new byte[1024];int len;long begin= System.currentTimeMillis();while((len = fis.read(buff))!= -1){fos.write(buff,0,len);}long end = System.currentTimeMillis();System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");fos.close();fis.close();}

使用每次读取一个字节数组的方式 实现图片的复制:

public static void main(String[] args) throws IOException {FileInputStream  fis = new FileInputStream("D:\\IO\\os\\fos.txt");//  int read(byte[] b)byte[] buff = new byte[10];int len;while( (len =fis.read(buff)) != -1){//每次读取10个字节  在去输出// 将读取到的字节数组转换为字符串 输出转换的时候 读几个就转换几个System.out.print(new String(buff,0,len));//}fis.close();
}

字节缓冲流(处理流)

BufferedInputStream 内部有一个缓冲区数组
当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次有多个字节
在这里插入图片描述
BufferedOutputStream 提供输出效率
在这里插入图片描述

//创建一个输出缓冲流  此处采用匿名对象创建一个OutputStream对象作为缓冲流的参数
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("buff.txt"));// 默认的缓冲区大小8192
bos.write("hello".getBytes());
bos.write("world".getBytes());//如果在写数据的时候  如果缓冲区没有写满,此时我们就必须强制刷新缓冲区  否则缓冲区的数据时不能写出到文件的
bos.flush();//强制刷新缓冲区
bos.close();// 在执行close之前会限执行flush

实现图片复制:

public static void main(String[] args) throws IOException {// 创建一个输入缓冲流InputStream in = new FileInputStream("d:\\IO\\os\\mn.jpg");BufferedInputStream bis = new BufferedInputStream(in);//创建一个输出缓冲流  此处采用匿名对象创建一个OutputStream对象作为缓冲流的参数BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("buff.jpg"));// 默认的缓冲区大小8192byte[] buff = new byte[1024];int len;long begin= System.currentTimeMillis();while((len = bis.read(buff)) != -1){bos.write(buff,0,len);}long end = System.currentTimeMillis();System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");//1 bos.close();bis.close();
}

使用缓冲流实现复制视频:

public class CopyVido {public static void main(String[] args) throws IOException {long  begin = System.currentTimeMillis();//byteArrayCopy();//3306buffStreamCopy();//1167long end =  System.currentTimeMillis();System.out.println(end - begin);}// 每次读取一个字节数组public static void  byteArrayCopy() throws IOException {FileInputStream  fis = new FileInputStream("d:\\IO\\os\\Java高级- 23.avi");FileOutputStream fos = new FileOutputStream("java23.avi");byte[] buff = new byte[1024 * 1024] ;int len ;while((len = fis.read(buff)) !=  -1){fos.write(buff,0,len);}fos.close();fis.close();}// 使用字节缓冲流来实现public  static void  buffStreamCopy() throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\IO\\os\\Java高级- 23.avi"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("java2323.avi"));byte[] buff = new byte[1024 * 1024] ;int len ;while((len = bis.read(buff)) !=  -1){bos.write(buff,0,len);}bis.close();bos.close();}
http://www.xdnf.cn/news/851725.html

相关文章:

  • 多爪抓取机械手结构设计
  • 01通讯录管理系统——系统功能介绍
  • 如何快速上手跨境电商ERP源码开发:简单易懂的教程
  • 深入研究Android启动速度优化(上)- 看完这些启动优化已经完成80%了
  • 美国口语俚语大全
  • 【图像拼接】基于SURF算法实现图像拼接附Matlab代码
  • output_buffering详细介绍
  • 微信小程序投票系统制作过程详解
  • 简单制作 OS X Yosemite 10.10 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
  • JetsonNano学习(五)JetsonNano 安装 PyTorch 及 Torchvision
  • 基于Java的基金交易网站系统设计与实现(源码+lw+部署文档+讲解等)
  • 另一个 OleDbParameterCollection 中已包含 OleDbParameter 错误分析及解决办法
  • 基于python语言的网页设计(手把手教你设计一个个人博客网站)
  • ASK,OOK,FSK,GFSK简介
  • 几个重要的电子元器件网站
  • 手写识别介绍
  • android:TableLayout表格布局详解
  • matlab usb接口编程,如何使用MATLAB进行USB2.0摄像头的编程
  • STC89C52引脚
  • 查看计算机启动项命令,启动项指令命令有哪些?教你设置电脑Windows开机启动项命令...
  • 玩机搞机---全网最详细的手机全机型 刷机教程一
  • PUBG绝地求生更新很慢、无法更新、更新不动的几个解决措施来了
  • 《Android框架揭秘》——导读
  • Windows2000、XP、2003系统万能Ghost全攻略
  • 用计算机测试生日,超准生日爱情配对测试
  • Android 1分钟教你打造酷炫的引导页(实现ViewPager淡入淡出切换)
  • Vissim4.3之API/SDK编程;Vissim编程;
  • 软件测试(接口测试、性能测试、自动化测试)详解
  • ultraedit
  • 插件界的全家桶,对接华为云能力就用它了