java中的IO流(字节流)

IO流概述

  • IO流用来处理设备之间的数据传输
  • Java对数据的操作通过流的方式
  • Java用于操作流的对象都在IO包中

IO流分类

按照数据流向

  • 输入流 读入数据
  • 输出流 写出数据

按照数据类型

  • 字节流 可以读写任何类型的文件,例如音频,视频,文本文件
  • 字节输入流 InputStream 读
  • 字节输出流 OutputStream 写
  • 字符流 只能读写文本文件
  • 字符输入流 Reader 读
  • 字符输出流 Writer 写

IO流基类概述

字节流的抽象基类

  • InputStream ,OutputStream

字符流的抽象基类

  • Reader , Writer

IO流的继承关系

FileOutputStream写出数据
构造方法

  • FileOutputStream(File file)
  • FileOutputStream(String name)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MyTest1 {
    public static void main(String[] args) throws FileNotFoundException {
        //创建一个向指定名称的文件中写入数据的输出文件流
        File file = new File("a.txt");
        //输出流所关联的文件如果不存在,会自动创建出来
        FileOutputStream outputStream = new FileOutputStream(file);
        //传入一个具体的文件路径名字符串
        FileOutputStream outputStream1 = new FileOutputStream("b.txt");

    }
}

创建字节输出流所作的几件事情

  1. 调用系统资源创建a.txt文件
  2. 创建一个fos对象
  3. 把fos对象指向这个文件

流释放掉要关闭的原因

  1. 通知系统释放关于管理a.txt文件的资源
  2. 让Io流对象变成垃圾,等待垃圾回收器对其回收

FileOutputStream的三个write()方法

  • public void write(int b)
    &emap;&emap;写入一个字节,超过一个字节,舍掉前面的字节

  • public void write(byte[] b)
    &emap;&emap;写一个字节数组

  • public void write(byte[] b,int off,int len)
    &emap;&emap;写一个字节数组的一部分

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyTest {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("a.txt");
        //一次写入一个字节,超过一个字节会自动丢弃掉多余字节
        out.write(1);
        //一次写入一个字节数组
        out.write(new byte[]{1,3,4,5});
        out.write("\r\n".getBytes());
        String str = "清清水,幽幽人";
        byte[] bytes = str.getBytes();
        out.write(bytes);
        //换行
        out.write("\r\n".getBytes());
        //一次写入一个字节数组的一部分
        out.write(bytes,0,4);
        out.write("\r\n".getBytes());
        //流使用完毕记得释放掉
        out.close();
    }
}

FileOutputStream写出数据实现换行和追加写入

  • windows下的换行符只用是 \r\n
  • Linux \n
  • Mac \r
import java.io.FileOutputStream;
import java.io.IOException;

public class MyTest2 {
    public static void main(String[] args) throws IOException {
    	//追加的时候后面添加一个true
        FileOutputStream out  = new FileOutputStream("a.txt",true);
        out.write("一二三四五".getBytes());
        out.write("\r\n".getBytes());//写入一个换行符
        out.write("上山打老虎".getBytes());
        out.write("\r\n".getBytes());//写入一个换行符
        out.write("老虎没打着".getBytes());
        out.write("\r\n".getBytes());//写入一个换行符
         out.close();
    }
}

FileInputStream读取数据一次一个字节

  • int read()
      一次读取一个字节,如果没有数据返回的就是-1.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyTest3 {
    public static void main(String[] args) throws IOException {
        FileInputStream in  = new FileInputStream("a.txt");
        FileOutputStream out = new FileOutputStream("b.txt");
        //一次读写一个字节效率较慢
        int len = 0;
        while ((len=in.read())!=-1){
            out.write(len);
        }
        in.close();
        out.close();
    }
}

FileInputStream读取数据一次一个字节数组
  返回的int类型的值表示的意思是读取到的字节的个数,如果没有数据了,就返回-1.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyTest4 {
    public static void main(String[] args) throws IOException {
  	   //关联源文件
        FileInputStream in = new FileInputStream("a.txt");
        //关联目标文件
        FileOutputStream out = new FileOutputStream("b.txt");
        //一次读取一个字节数组
        byte[] bytes = new byte[1024 * 8];
        int len = 0;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
        in.close();
        out.close();
    }
}

字节缓冲流

BufferedOutputStream写出数据

   字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是因为其加入了数组这样的缓冲区,所以有了字节缓冲区流

构造方法

  • BufferedOutputStream(OutputStream out)

BufferedInputStream读取数据
构造方法

  • BufferedInputStream(InputStream in)

下面利用高效的字节缓冲区流进行复制

import java.io.*;

public class MyTest5 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("a.txt"));
        BufferedOutputStream out  = new BufferedOutputStream(new FileOutputStream("b.txt"));
        byte[] bytes = new byte[1024 * 8];
        int len = 0;
        while ((len = in.read(bytes)) != -1) {
            out.write(bytes, 0, len);

        }
        in.close();
        out.close();


    }
}

流的异常处理流程

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyTest6 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream("a.txt");
            out = new FileOutputStream("b.txt");
            byte[] bytes = new byte[1024 * 8];
            int len = 0;
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值