Java入门10(IO流)

IO流(input/output)

​ 数据运输的载体或者中间键

字节流

输入字节流(FileInputStream)

​ 以字节为最小单元,读取任何类型的文件,但是要注意字符集类型的转换。

public static void testFileInputStream(){
 // 获取文件对象
 File f = new File("文件绝对路径balabala");
 // 创建字节输入流
 try {
 FileInputStream fls = new FileInputStream(f);
 // 准备一个数组,用来接收一会从流中读取的数据
 byte[] data = new byte[(int)f.length()];
 // 将数据从流中读取,存储在字节数组中
 fls.read(data);
 // 遍历输出字节数组
 // 如果读取的是文本,那么文本在计算机底层存储的是字符集对应的编码,所以要及逆行强制转换
 for (byte b : data) {
 System.out.print((char)b);
 }
 }catch (IOException e){
 System.out.println("IO流异常");
 }
}

输出字节流(FileOutputStream)

​ 如果输出流在创建的时候,如果文件对象在磁盘中不存在,那么将自动创建一个相应文件

public static void testFileOutputStream(){
 File f = new File("文件绝对路径");
 try{
 // 创建字节输出流,默认是覆盖,而不是拼接内容
 // 覆盖:
 FileOutputStream fos = new FileOutputStream(f);
 // 拼接:
 // FileOutputStream fos = new FileOutputStream(f,true);
 
 // 在Java中准备一个数据,用于数据的输出(test01)
 byte[] data = "test01".getBytes();
 // 向文件写入。。。
 fos.write(data);
 // 在流使用完成之后进行关闭
 fos.close();
 }catch(IOException e){
 System.out.println("IO流异常!");
 }
}

字节流关闭(try with)

​ 在JDK1.7之后提供了try with语句块,如果在try with块中声明的对象,实现了AutoCloseable接口,那么这个对象会在try catch语句运行完成之后,进行资源的自动释放

// 修改后的文件输出流demo
public static void testFileOutputStream(){
 File f = new File("文件绝对路径");
 // JDK 在1.7之后提供了try with语句块
 // 如果在try with块中声明的对象,实现了AutoCloseable接口
 // 那么这个对象会在try catch语句运行完成之后,进行资源的自动释放
 try(FileOutputStream fos = new FileOutputStream(f,true);){
 // 在Java中准备一个数据,用于数据的输出(test01)
 byte[] data = "test02".getBytes();
 // 向文件写入。。。
 fos.write(data);
 }catch(IOException e){
 System.out.println("IO流异常!");
 }
}

字符流

​ 在读取数据的时候,会根据JVM定义的默认字符集,对读取的数据进行编码转换

字符输入流(FileReader)

public static void testFileReader(){
 File f = new File("文件绝对路径");
 try(FileReader fr = new FileReader(f)){
 char[] data = new char[(int)f.length()];
 fr.read(data);
 // 遍历字符数组: 如果文件中出现中文,那么文件的字节大小一定大于文件字符的数量,定义的data数组在读取完数据之后一定有空位
 // 遍历的时候要跳过这些空字符
 loop:for (char c : data) {
 if(c == '\u0000'){
 break loop;
 }
 System.out.print(c);
 }
 }catch(IOException e){
 System.out.println("IO流异常!");
 }
}

字符输出流(FileWriter)

public static void testFileWriter(){
 File f = new File("文件绝对路径");
 // 同样可以通过提供第二个参数来指定是覆盖还是补充目标文件
 try(FileWriter fw = new FileWriter(f)){
 char[] data = "这是一个测试样例\n".toCharArray();
 // 字符输出。。。
 fw.write(data);
 }catch(IOException e){
 System.out.println("IO流异常!");
 }
}

字符流和字节流的弊端

  1. 这两种流在读写的时候,分别以字符或字节作为单位,如果读取的文件过大,就会影响磁盘的IO(input,output)性能
  2. 为了避免过多的IO操作,我们建立了缓存的机制,在读取数据的时候,是一次性将较多的数据读取到硬盘当中,后续的读取都从缓存进行读取,直到缓存的数据读取完,在重新从硬盘读取数据
  3. 写入数据也是同样的道理,先将数据写到缓存中,再将缓存一次性提交
  4. 通过减少写入磁盘的次数达到提高效率的目的

缓存流

缓存输入流(BufferedReader)

public static void testBufferedReader(){
 File f = new File("文件绝对路径");
 // 先创建文件流对象,再通过文件流创建缓存流对象
 try(FileReader fr = new FileReader(f);BufferedReader br = new BufferedReader(fr)){
 // 一般来讲会使用readline方法进行整行读取
 // 整行读取的时候默认去掉行末的换行符
 String line = "";
 // 判断line是否为空,读取到了文件最后
 while((line = br.readLine())!=null){
 System.out.println(line);
 }
 }catch(IOException e){
 System.out.println("IO流异常!");
 }
}

缓存输出流(PrintWriter)

public static void testPrintWriter(){
 File f = new File("文件绝对路径");
 // 缓存输出再写入数据的时候,如果没有调用flush方法进行提交
 // 则会再JVM退出之前进行提交
 // 如果你添加了这个autoFlush参数,每写入一行数据,都会自动将缓存提交给硬盘
 try(FileWriter fw = new FileWriter(f,true);PrintWriter pw = new PrintWriter(fw,true)){
 // 缓存输出。。。
 pw.println("这是一个测试样例!");
 }catch(IOException e){
 System.out.println("IO流异常!");
 }
}

对象流

序列化&反序列化

​ 序列化:将Java对象转换为特殊的字节码,或者是字符串,用于存储或者传输

​ 反序列化:将特殊的字节码或者是字符串,还原成Java对象的过程

定义一个测试类

​ ⭐如果某个类可以被序列化,那么这个类一定要实现Serializable接口!

public class Student implements Serializable {
 // 如果某个类可以被序列化,那么这个类一定要实现Serializable接口
 private String name;
 private int age;
 // 如果类被序列化,类中必须有一个最终静态常量,如果后续类添加了成员变量,或者更新了方法,都必须就该版本号
 private static final long serialVersionUID = 1L;
}

对象流序列化(ObjectStream)

public static void testObjectStream(){
 // 创建一个对象,用来测试序列化,反序列化
 Student student01 = new Student("robot01",18);
 // 提供一个文件用于保存对象序列化之后的字节码
 File f = new File("文件绝对路径");
 // 创建对象流
 try(FileOutputStream fos = new FileOutputStream(f);ObjectOutputStream oos = new ObjectOutputStream(fos);
 FileInputStream fis = new FileInputStream(f);ObjectInputStream ois = new ObjectInputStream(fis);
 ){
 // 将学生对象序列化
 oos.writeObject(student01);
 // 将学生对象反序列化
 Student student02 = (Student)ois.readObject();
 System.out.println(student02);
 }catch(Exception e){
 System.out.println("IO流异常!");
 }
}

数据流

​ 数据流基于字节流实现,可以在存储和读取数据的时候,指定数据类型,数据输出流在输出数据时,会进行加密,只能用数据输入流进行读取

数据输入流(DataInputStream)

public static void testDataInputStream(){
 File f = new File("文件绝对路径");
 try(FileInputStream fis = new FileInputStream(f);DataInputStream dis = new DataInputStream(fis)){
 System.out.println(dis.readUTF());
 System.out.println(dis.readInt());
 }catch(Exception e){
 System.out.println("IO流异常!");
 }
}

数据输出流(DataOutPutStream)

public static void testDataOutPutStream(){
 File f = new File("文件绝对路径");
 try(FileOutputStream fos = new FileOutputStream(f);DataOutputStream dos = new DataOutputStream(fos)){
 dos.writeUTF("这是一个测试样例!");
 dos.writeInt(10086);
 }catch(Exception e){
 System.out.println("IO流异常!");
 }
}

(Demo)通过url实现Java文件下载以及文件批量下载

public static void testDownload(){
 // 提前定义变量,提升变量的作用域
 HttpURLConnection connection = null;
 InputStream is = null;
 FileOutputStream fos = null;
 try{
 String urlPath = "https://dldir1.qq.com/qqfile/qq/TIM3.4.7/TIM3.4.7.22084.exe";
 // 实例化url对象
 URL url = new URL(urlPath);
 // 基于url获取与目标服务器的连接
 connection = (HttpURLConnection) url.openConnection();
 // 根据连接对象获取用力啊接收下载数据的输入流
 is = connection.getInputStream();
 // 获取下载文件的文件名
 String downloadName = urlPath.substring(urlPath.lastIndexOf("/") + 1);
 // 创建对应的文件对象
 File f = new File("目的文件夹绝对路径 + \\" + downloadName);
 // 创建输出流对象
 fos = new FileOutputStream(f);
 // 这里不需要等待下载好,再输出,尝试边下载遍写入
 // 1. 准备一个字节数组作为一个缓存(1KB)
 byte[] buffer = new byte[1024];
 // 2. 通过输入流将数据读取到buffer这个作为缓存的字节数组中
 // 加入循环了n次,循环第1~(n-1)次时length=1024,循环第n次时,length<=1024
 // 当全部读完之后,length = -1 退出循环
 int length;
 while((length = is.read(buffer))!=-1){
 // buffer:输出数据的来源
 // off:表示从数组的哪一个下标开始读取
 // length:希望写入的字节数
 fos.write(buffer,0,length);
 }
 System.out.println("下载完成!");
 }catch(Exception e){
 e.printStackTrace();
 }finally {
 // 草率的关闭一下流以及连接
 try {
 is.close();
 connection.disconnect();
 fos.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
}
作者:te9uila原文地址:https://www.cnblogs.com/te9uila/p/17436984.html

%s 个评论

要回复文章请先登录注册