Java-进阶:Java-File-&-IO,裁员被逼啃透Java22个技术点

    //创建字节输出流,绑定文件
    FileOutputStream fos = new FileOutputStream("c:\\utf.txt");
    //创建转换流对象,构造方法保证字节输出流,并指定编码表是UTF-8
    OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); //GBK可以不写
    osw.write("你好");
    osw.close(); //使用close()连刷新带关闭
}

}

### 2. InputStreamReader 类
#### (1) 概述

- ```java.io.InputStreamReader```继承 **Reader**;是 **字符输入流**,读取文本文件
- 字节流向字符的桥梁,将 **字节流** 转为 **字符流**
- 读取的方法:**read()** 读取1个字符,读取字符数组
#### (2) 构造方法

- ```InputStreamReader(InputStream in)```:接收所有的 **字节输入流**
>可以传递的字节输入流: FileInputStream

- ```InputStreamReader(InputStream in,String charsetName) ```: 传递编码表的名字
```java
public class InputStreamReaderDemo {
    public static void main(String[] args) throws IOException {
        readUTF();
    }

    //转换流,InputSteamReader读取文本,采用UTF-8编码表,读取文件utf
    public static void readUTF()throws IOException{
        //创建字节输入流,传递文本文件
        FileInputStream fis = new FileInputStream("c:\\utf.txt");
        //创建转换流对象,构造方法中,包装字节输入流,同时写编码表名
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        char[] ch = new char[1024];
        int len = isr.read(ch);
        System.out.println(new String(ch,0,len));
        isr.close();
    }
}

###3. 转换流子类父类的区别

  • 继承关系
    OutputStreamWriter 的子类: FileWriter
    InputStreamReader 的子类:FileReader
  • 区别
  • OutputStreamWriterInputStreamReader 是字符和字节的桥梁:也可以称之为字符转换流。字符转换流原理:字节流 + 编码表
  • FileWriterFileReader:作为子类,仅作为操作字符文件的便捷类存在。当操作的字符文件,使用的是默认编码表时可以不用父类,而直接用子类就完成操作了,简化了代码。
  • 以下三句话功能相同

InputStreamReader isr = new InputStreamReader(new FileInputStream(“a.txt”));//默认字符集。
InputStreamReader isr = new InputStreamReader(new FileInputStream(“a.txt”),“GBK”);//指定GBK字符集。
FileReader fr = new FileReader(“a.txt”);

注意:一旦要指定其他编码时,绝对不能用子类,必须使用字符转换流。什么时候用子类呢?
条件:1、操作的是文件。2、使用默认编码。

二、字节缓冲流

1. 概述

  • 可提高IO流的读写速度
  • 分为字节缓冲流与字符缓冲流

2. 字节输出缓冲流 BufferedOutputStream

  • java.io.BufferedOuputStream作用: 提高原有输出流的写入效率
  • BufferedOuputStream 继承 OutputStream
  • 方法:写入 write 字节,字节数组
  • 构造方法BufferedOuputStream(OuputStream out):可以传递任意的字节输出流,传递的是哪个字节流,就对哪个字节流提高效率
public class BufferedOutputStreamDemo {
   
    public static void main(String[] args)throws IOException {
   
        //创建字节输出流,绑定文件
        //FileOutputStream fos = new FileOutputStream("c:\\buffer.txt");

        //创建字节输出流缓冲流的对象,构造方法中,传递字节输出流
        BufferedOutputStream bos = new
                BufferedOutputStream(new FileOutputStream("c:\\buffer.txt"));

        bos.write(55);
        byte[] bytes = "HelloWorld".getBytes();
        bos.write(bytes);
        bos.write(bytes, 3, 2);

        bos.close();
    }
}

3. 字节输入缓冲流 BufferedInputStream

  • BufferedInputStream,继承 InputStream ,标准的字节输入流
  • 读取方法: read() ,单个字节,字节数组
  • 构造方法: BufferedInputStream(InputStream in):可以传递任意的字节输入流,传递是谁,就提高谁的效率
  • 可以传递的字节输入流 FileInputStream
public class BufferedInputStreamDemo {
   
    public static void main(String[] args) throws IOException{
   
        //创建字节输入流的缓冲流对象,构造方法中包装字节输入流,包装文件
        BufferedInputStream bis = new
                BufferedInputStream(new FileInputStream("c:\\buffer.txt"));
        byte[] bytes = new byte[1024];
        int len = 0 ;
        while((len = bis.read(bytes))!=-1){
   
            System.out.print(new String(by
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值