字节流-FileInputStream_FileOutputStream_BufferedInputStream_BufferedOutputStream
来源:互联网 发布:淘宝客乱扣佣金 编辑:程序博客网 时间:2024/06/05 14:54
20.01_IO流(IO流概述及其分类)
- 1.概念
- IO流用来处理设备之间的数据传输
- Java对数据的操作是通过流的方式
- Java用于操作流的类都在IO包中
- 流按流向分为两种:输入流,输出流。
- 流按操作类型分为两种:
- 字节流 : 字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
- 字符流 : 字符流只能操作纯字符数据,比较方便。
- 2.IO流常用父类
- 字节流的抽象父类:
- InputStream
- OutputStream
- 字符流的抽象父类:
- Reader
- Writer
- 字节流的抽象父类:
- 3.IO程序书写
- 使用前,导入IO包中的类
- 使用时,进行IO异常处理
- 使用后,释放资源
20.02_IO流(FileInputStream)
- read()一次读取一个字节
public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("xxx.txt"); //创建流对象 int b; while((b=fis.read())!=-1){ //从硬盘上读取一个字节,读到文件结尾,返回-1 System.out.println(b); } fis.close(); //关闭流对象 }
20.03_IO流(read()方法返回值为什么是int)
- read()方法读取的是一个字节,为什么返回是int,而不是byte
-
因为字节输入流可以操作任意类型的文件,比如图片音频等,这些文件底层都是以二进制形式的存储的,如果每次读取都返回byte,有可能在读到中间的时候遇到111111111
那么这11111111是byte类型的-1,我们的程序是遇到-1就会停止不读了,后面的数据就读不到了,所以在读取的时候用int类型接收,如果11111111会在其前面补上
24个0凑足4个字节,那么byte类型的-1就变成int类型的255了这样可以保证整个数据读完,而结束标记的-1就是int类型
20.04_IO流(FileOutputStream)
- write()一次写出一个字节
public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("yyy.txt"); //创建流对象 int b; while((b=fis.read())!=-1){ //从硬盘上读取一个字节,读到文件结尾,返回-1 System.out.println(b); //这里读取的一个字节,前面补24个0,凑成4个字节,转为int } fis.close(); //关闭流对象 FileOutputStream fos = new FileOutputStream("yyy.txt"); //创建写流对象,如果文件不存在,则创建 fos.write(97); //写入一个字节,写入时会去掉前面补的24个0,则作为一个字节存入 fos.write(98); fos.write(99); fos.close(); }
20.05_IO流(FileOutputStream追加)
- A:案例演示
- FileOutputStream的构造方法写出数据如何实现数据的追加写入
FileOutputStream在创建对象的时候,如果没有这个文件就帮我创建出来,如果有这个文件,就会先将文件清空,再写入。在第二个参数传true,则是继续追加。
-
FileOutputStream fos = new FileOutputStream(“bbb.txt”,true); //如果没有bbb.txt,会创建出一个
//fos.write(97); //虽然写出的是一个int数,但是在写出的时候会将前面的24个0去掉,所以写出的一个byte
fos.write(98);
fos.write(99);
fos.close();
20.06_IO流(拷贝图片)
- FileInputStream读取
- FileOutputStream写出
IO流核心代码:
FileInputStream fis = new FileInputStream("致青春.mp3"); //创建输入流对象,关联致青春.mp3FileOutputStream fos = new FileOutputStream("copy.mp3");//创建输出流对象,关联copy.mp3int b;while((b = fis.read()) != -1) { fos.write(b);}fis.close();fos.close();
20.07_IO流(拷贝音频文件画原理图)
- A:案例演示
- 字节流一次读写一个字节复制音频
- 弊端:效率太低
20.08_IO流(字节数组拷贝之available()方法)
- A:案例演示
- int read(byte[] b):一次读取一个字节数组
- write(byte[] b):一次写出一个字节数组
- available()获取读的文件所有的字节个数
- 弊端:有可能会内存溢出
//一次性读取到字节数组中,但是当文件过大时,会发生内存溢出 FileInputStream fis = new FileInputStream("致青春.mp3"); FileOutputStream fos = new FileOutputStream("copy.mp3"); byte[] arr = new byte[fis.available()]; //根据文件大小做一个字节数组 fis.read(arr); //将文件上的所有字节读取到数组中 fos.write(arr); //将数组中的所有字节一次写到了文件上 fis.close(); fos.close();
20.09_IO流(定义小数组)
- write(byte[] b)
- write(byte[] b, int off, int len)写出有效的字节个数
20.10_IO流(定义小数组的标准格式)(可以)
A:案例演示
字节流一次读写一个字节数组复制图片和视频
FileInputStream fis = new FileInputStream(“致青春.mp3”);
FileOutputStream fos = new FileOutputStream(“copy.mp3”);
int len;
byte[] arr = new byte[1024 * 8]; //自定义字节数组,1024的整数倍while((len = fis.read(arr)) != -1) {
//fos.write(arr);
fos.write(arr, 0, len); //写出字节数组写出有效个字节个数
}fis.close();
fos.close();
20.11_IO流(BufferedInputStream和BufferOutputStream拷贝)(可以)
- A:缓冲思想
- 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,
- 这是加入了数组这样的缓冲区效果,java本身在设计的时候,
- 也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流
- B.BufferedInputStream
- BufferedInputStream内置了一个缓冲区(数组)
- 从BufferedInputStream中读取一个字节时
- BufferedInputStream会一次性从文件中读取8192个, 存在缓冲区中, 返回给程序一个
- 程序再次读取时, 就不用找文件了, 直接从缓冲区中获取
- 直到缓冲区中所有的都被使用过, 才重新从文件中读取8192个
- C.BufferedOutputStream
- BufferedOutputStream也内置了一个缓冲区(数组)
- 程序向流中写出字节时, 不会直接写到文件, 先写到缓冲区中
- 直到缓冲区写满, BufferedOutputStream才会把缓冲区中的数据一次性写到文件里
D.拷贝的代码
FileInputStream fis = new FileInputStream("致青春.mp3"); //创建文件输入流对象,关联致青春.mp3BufferedInputStream bis = new BufferedInputStream(fis); //创建缓冲区对fis装饰FileOutputStream fos = new FileOutputStream("copy.mp3"); //创建输出流对象,关联copy.mp3BufferedOutputStream bos = new BufferedOutputStream(fos); //创建缓冲区对fos装饰int b;while((b = bis.read()) != -1) { bos.write(b);}bis.close(); //只关装饰后的对象即可bos.close();
E.小数组的读写和带Buffered的读取哪个更快?
- 定义小数组如果是8192个字节大小和Buffered比较的话
- 定义小数组会略胜一筹,因为读和写操作的是同一个数组
- 而Buffered操作的是两个数组
20.12_IO流(flush和close方法的区别)
- flush()方法
- 用来刷新缓冲区的,刷新后可以再次写出
- close()方法
- 用来关闭流释放资源的的,如果是带缓冲区的流对象的close()方法,不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出
20.13_IO流(字节流读写中文)
- 字节流读取中文的问题
- 字节流在读中文的时候有可能会读到半个中文,造成乱码
- 字节流写出中文的问题
- 字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组
- 写出回车换行 write(“\r\n”.getBytes());
/** * 字节流操作中文,可能出现乱码 * 读的时候,会出现乱码,但是写的时候没事 * 解决办法:字符流操作 * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //Demo1(); FileOutputStream fos = new FileOutputStream("yyy.txt"); fos.write("你好你好啦啦啦".getBytes()); fos.write("\r\n".getBytes()); } private static void Demo1() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("xxx.txt"); FileOutputStream fos = new FileOutputStream("yyy.txt"); byte[] arr = new byte[4]; int len; while((len=fis.read(arr))!=-1){ fos.write(arr,0,len); System.out.println(new String(arr,0,len)); } fis.close(); fos.close(); }
20.14_IO流(流的标准处理异常代码1.6版本及其以前)
try finally嵌套,,,尽可能关闭流
FileInputStream fis = null;FileOutputStream fos = null;try { fis = new FileInputStream("aaa.txt"); fos = new FileOutputStream("bbb.txt"); int b; while((b = fis.read()) != -1) { fos.write(b); }} finally { try { if(fis != null) fis.close(); }finally { if(fos != null) fos.close(); }}
20.15_IO流(流的标准处理异常代码1.7版本)
- try close
public class Demo5_FileInputStream { public static void main(String[] args) throws Exception { try( FileInputStream fis = new FileInputStream("xxx.txt"); FileOutputStream fos = new FileOutputStream("yyy.txt"); MyClose mc = new MyClose(); ){ int b; while((b=fis.read())!=-1){ fos.write(b); } } //把流写在try的()里,{}执行完,会自动关闭, //因为FileInputStream,FileOutputStream都实现了AutoCloseable里的close(), //所以我自定义MyClose类,实现AutoCloseable里的close(),也可以自动关闭了。 }}class MyClose implements AutoCloseable{ @Override public void close() throws Exception { System.out.println("我关了"); }}
- 原理
- 在try()中创建的流对象必须实现了AutoCloseable这个接口,如果实现了,在try后面的{}(读写代码)执行后就会自动调用,流对象的close方法将流关掉
20.16_IO流(图片加密)
给图片加密
将写出的字节异或上一个数,这个数就是秘钥,解密的时候再异或上这个数就可以了。
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(“a.jpg”));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(“b.jpg”));int b;while((b = bis.read()) != -1) { bos.write(b ^ 123);}bis.close();bos.close();
20.17_IO流(拷贝文件)
- 在控制台录入文件的路径,将文件拷贝到当前项目下
package com.hongwei.FileStream;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Scanner;public class Demo6_FileInputStream { /** * 在控制台录入文件的路径,将文件拷贝到当前项目下 * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File file = getFile(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName())); int len; byte[] arr =new byte[8192]; while((len=bis.read(arr))!=-1){ bos.write(arr,0,len); } bos.close(); bis.close(); } public static File getFile(){ System.out.println("请输入文件路径:"); Scanner sc = new Scanner(System.in); while(true){ String line = sc.nextLine(); File file = new File(line); if(!file.exists()){ System.out.println("文件路径不存在!请重新输入文件路径:"); }else if(file.isDirectory()){ System.out.println("您输入的是文件夹,请重新输入文件路径:"); }else{ return file; } } }}
20.18_IO流(录入数据拷贝到文件)
- 将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出
public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); FileOutputStream fos = new FileOutputStream("text.txt"); System.out.println("请输入数据:"); while(true){ String line = sc.nextLine(); if("quit".equals(line)){ break; }else{ fos.write(line.getBytes()); fos.write("\r\n".getBytes()); } } fos.close(); }
fos.close();
20.19_day20总结
- 把今天的知识点总结一遍。
- 字节流-FileInputStream_FileOutputStream_BufferedInputStream_BufferedOutputStream
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- 字节流
- Java基本数据类型
- Ajax导出Excel的做法流程
- 蓝桥杯 第39阶台阶
- vim中空格替换为换行及删除换行
- 微信硬件平台设备直连全讲解(1)开发前的准备
- 字节流-FileInputStream_FileOutputStream_BufferedInputStream_BufferedOutputStream
- Makefile详解——从入门到精通
- gitlib项目迁移到新Gitlab Server
- Android之Adapter的封装与抽象(二)
- arcengine 捕捉笔记
- 【转】一个十二年老程序猿的碎碎念
- SQL 字符串排序 (字符串为数字、字母、汉字)
- 查看Qt中可用的数据库插件
- Android 时间为隔天的九点、并且跳过周末