Java之IO流详解(二)——字节流

一、IO流

  1. IO流概述

(1)IO:Input读取数据(输入)、Output写数据(输出)。
(2)流:一种抽象概念,是对数据传输的总称,数据在设备间的传输称为流,流的本质是数据传输,IO流就是用来处理设备间数据传输问题的。
(3)常见应用:文件的上传、下载、复杂等

  1. IO流的分类

(1)根据数据流向分类:
输入流:读数据,将硬盘中的数据读取到内存中;
输出流:写数据,将程序中的数据写入到硬盘中。

(2)根据数据类型分类:
字节流:字节输入流、字节输出流;
字符流:字符输入流、字符输出流。

(3)IO流应用场景
纯文本文件,优先使用字符流;
图片、视频、音频等二进制文件,优先使用字节流;
不确定文件类型,优先使用字节流,因为字节流是万能的流。

二、字节流写入数据

  1. 概述

1、InputStream:这是个抽象类,是字节输入流的所有类的超类。
2、OutputStream:这是个抽象类,是字节输出流的所有类的超类。
3、FileOutputStream:文件输出流,用于将数据写入FIle。
4、FileOutputStream:创建文件输出流以指定的名称写入文件。

  1. 写入方法
方法说明
void write(int b)将指定的字节写入此文件输出流,一次写一个字节数据
void write(byte[] b)将b.length字节从指定的字节数组写入此文件输出流,一次写一个字节数据
void write(byte[] b, int off, int len)将len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流,一次写一个字节数组的部分数据

eg:

public static void main(String[] args) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
    //void write(int b) 将指定的字节写入此文件输出流,一次写一个字节数据
    fileOutputStream.write(97);
    fileOutputStream.close();
}
public static void main(String[] args) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
    //void write(byte[] b) 将b.length字节从指定的字节数组写入此文件输出流,一次写一个字节数据
    byte[] bytes = {97,98,99,100};
    fileOutputStream.write(bytes);
    fileOutputStream.close();
}
public static void main(String[] args) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
    //void write(byte[] b, int off, int len) 将len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流,一次写一个字节数组的部分数据
    byte[] bytes = {97,98,99,100};
    fileOutputStream.write(bytes,1,bytes.length-1);
    fileOutputStream.close();
}
public static void main(String[] args) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
    String str = "chenzeyuhaohaoxiexitiantianxiangshang";
    fileOutputStream.write(str.getBytes());
    fileOutputStream.close();
}
  1. 写入数据换行、追加写数据

(1)字节流写入数据换行:

windows系统的换行符:\r\n
linux系统的换行符:\n
mac系统的换行符:\r

eg:

//写入数据换行
public static void main(String[] args) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
    String str = "chenzeyu";
    for (int i=0; i<10; i++){
        fileOutputStream.write(str.getBytes());
        fileOutputStream.write("\r\n".getBytes());
    }
    fileOutputStream.close();
}

(2)字节流写入数据通过new FileOutputStream(new File(“C:\Users\Administrator\Desktop\test\test01\test.txt”),true);表示追加写入数据,如果第二个参数为true,则表示字节流写入文件的末尾。

eg:

//追加写数据
public static void main(String[] args) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt",true);
    String str = "qizekj";
    for (int i=0; i<10; i++){
        fileOutputStream.write(str.getBytes());
        fileOutputStream.write("\r\n".getBytes());
    }
    fileOutputStream.close();
}
  1. 字节流写入数据异常的处理
    在异常处理时,是在finally中执行close方法来释放资源。
    eg:
public static void main(String[] args) {
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
        String str = "qizekj";
        for (int i=0; i<10; i++){
            fileOutputStream.write(str.getBytes());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }  finally {
        if(fileOutputStream != null){
            try {
                //释放资源
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

三、字节流读取数据

  1. 概述

1、FileInputStream:从文件中获取输入字节。
2、FileInputStream:通过打开与实际文件的连接来创建一个FileInputStream,该文件由路径名name命令。

  1. 常用方法
方法说明
int read()从该输入流读取一个字节数据,返回值为 -1 时说明文件读取完毕

eg:

public static void main(String[] args) {
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test\\\\test01\\\\test.txt");
        int read;
        while ((read = fileInputStream.read()) != -1){
            System.out.print((char) read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(fileInputStream != null){
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  1. 字节流以字节数组的读取数据

eg:

public static void main(String[] args) {
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test\\\\test01\\\\test.txt");
        byte[] bytes = new byte[1024];
        int read;
        while ((read = fileInputStream.read(bytes)) != -1){
            System.out.print(new String(bytes,0, read));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(fileInputStream != null){
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

四、字节流复制数据

  1. 复制文本数据

步骤:
(1)使用FileInputStream把文件内容读到程序内存中;
(2)使用FileOutputStream把内容写入到另一个文件中。
eg:

public static void main(String[] args) {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
        fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test02\\test.txt");
        int read;
        while ((read = fileInputStream.read())!= -1){
            fileOutputStream.write(read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(fileInputStream != null){
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fileOutputStream != null){
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  1. 字节流复制图片

eg:

public static void main(String[] args) {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.jpg");
        fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test02\\test.jpg");
        byte[] bytes = new byte[1024];
        int read;
        while ((read = fileInputStream.read(bytes))!= -1){
            fileOutputStream.write(bytes,0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(fileInputStream != null){
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fileOutputStream != null){
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

五、字节缓冲流

1、使用字节流一个字节一个字节读取或者写入数据,会频繁的发送系统内核调用,所以效率非常低。这时可以使用字节缓冲流,缓冲区是一个内存区域的概念,类似于以“块”的形式写入或者读取数据,减少系统调用频率。
2、BufferedInputStream(InputStream in):字节缓冲输入流。
3、BufferedOutputStream(OutputStream out):字节缓冲输出流。
4、字节缓冲流的缓冲区大小默认是8K,即8192字节。

public static void main(String[] args) {
    //字节缓冲流写数据
    BufferedOutputStream bufferedOutputStream = null;
    try {
        bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test\\\\test01\\\\test.txt"));
        bufferedOutputStream.write("hao hao xue xi tian tian xiang shang".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(bufferedOutputStream != null){
            try {
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //字节缓冲流读数据
    BufferedInputStream bufferedInputStream = null;
    try {
        bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\test\\\\test01\\\\test.txt"));
        int length;
        byte[] bytes = new byte[1024];
        while ((length = bufferedInputStream.read(bytes))!= -1){
            System.out.println(new String(bytes, 0, length));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(bufferedInputStream != null){
            try {
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 字节缓冲流复制视频
    eg:
public static void main(String[] args) {
    BufferedInputStream bufferedInputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {
        bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\Wang Wang Team.qlv"));
        bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test02\\Wang Wang Team.qlv"));
        byte[] bytes = new byte[1024];
        int length;
        while ((length = bufferedInputStream.read(bytes)) != -1){
            bufferedOutputStream.write(bytes,0, length);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(bufferedInputStream != null){
            try {
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(bufferedOutputStream != null){
            try {
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

六、编码与解码、乱码

  • 汉字是GBK编码,则占用2个字节; 汉字是UTF-8编码,则占用3个字节。
  • 当字节流读取存储中文数据时可能会乱码,这时可以用字符流读取存储。 字符流=字节流+编码表。
  • 编码和解码使用的码表一定要一致,否则会导致乱码。
  • 编码表(ASC II)就是生活中的字符和计算机二进制的对照关系表。 如a -> 97。
  • Unicode:世界计算机协会制定通用的编码表,2个字节表示一个字符。
  • UTF-8:Unicode编码表的升级版,汉字基本都是3个字节。
  • GB2312:识别数千个中文。
  • GBK:识别2万多中文,2个字节表示一个字符。
  • GB18030:GBK的升级版。
  • ISO8859-1:欧洲制定的编码表。兼容ASCII。多了一些欧洲的语言,它用1个字节的全部来表示数据。没有未知字符的!。

编码:

方法说明
byte[] getBytes()使用平台的默认字符集将此String编码为byte数组
byte[] getBytes(String charsetName)使用指定的字符集将此String编码为byte数组

解码:

方法说明
String(byte[] bytes)使用平台的默认字符集解码指定的byte数组,构造一个新的String
String(byte[] bytes, String charsetName)使用指定的字符集解码指定的byte数组,构造一个新的String

eg:

public static void main(String[] args) {
    FileOutputStream fileOutputStream = null;
    FileInputStream fileInputStream = null;
    try {
        String str = "好好学习,天天向上";
        byte[] bytes = str.getBytes("UTF-8");
        fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
        fileOutputStream.write(bytes);
        fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test.txt");
        byte[] bytes2 = new byte[1024];
        int length;
        while ((length = fileInputStream.read(bytes2)) != -1){
            System.out.println(new String(bytes2,0, length, "UTF-8"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(fileInputStream != null){
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吟诗作对歌一曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值