JavaDay14-字节缓冲流、字符缓冲流、字符转换流

一、IO流体系

  • 字节流
    • InputStream
      • FileInputStream(基础流)
      • BufferedInputStream(高级流)
    • OutputStream
      • FileOutputStream(基础流)
      • BufferedOutputStream(高级流)
  • 字符流
    • Reader
      • FileReader(基础流)
      • BufferedReader(高级流)
      • InputStreamReader(转换输入流)(高级流)
    • Writer
      • FileWriter(基础流)
      • BufferedWriter(高级流)
      • OutputStreamWriter(转换输出流)(高级流)

二、缓冲流

  • 字节缓冲流
    • BufferedInputStream(高级流)
    • BufferedOutputStream(高级流)
import java.io.*;
//字节缓冲流-拷贝文件(一次读写一个字节)
public class BufferedDemo01 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bfis = new BufferedInputStream(new FileInputStream(""));
        BufferedOutputStream bfos = new BufferedOutputStream(new FileOutputStream(""));
        int b;
        while ((b=bfis.read())!=-1){
            bfos.write(b);
        }
        bfos.close();//不需要关闭基本流,缓冲流的底层代码会对基本流进行关闭
        bfis.close();
    }
}
import java.io.*;
//字节缓冲流-拷贝文件(一次读写多个字节)
public class BufferedDemo02 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bfis = new BufferedInputStream(new FileInputStream(""));
        BufferedOutputStream bfos = new BufferedOutputStream(new FileOutputStream(""));
        byte[] bytes = new byte[1024];
        int len;
        while ((len= bfis.read(bytes))!=-1){
            bfos.write(bytes,0,len);
        }
        bfos.close();
        bfis.close();
    }
}
  • 字符缓冲流
    • BufferedReader(高级流)
    • BufferedWriter(高级流)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
//字符缓冲流-输入流
public class BufferedDemo03 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(""));
        String s = br.readLine();//一次阅读一行,读到\r\n为止,但不会把\r\n读到内存中
        //循环读取
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
    }
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
//字符缓冲流-输出流
public class BufferedDemo04 {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("", true));//续写开关要写在基本流中
        bw.write(1);
        bw.newLine();//换行
        bw.write("wangfan");
        bw.close();//不需要关闭基本流,BufferedWriter底层代码会关
    }
}
import java.io.*;
import java.util.Arrays;
/*
需求:实现一个验证程序运行次数的小程序,要求如下:
1.当程序运行超过3次时给出提示:本软件只能免费使用3次,欢迎您注册会员后继续使用~
2.程序运行演示如下:
第一次运行控制台输出:欢迎使用本软件,第1次使用免费~
第二次运行控制台输出:欢迎使用本软件,第2次使用免费~
第三次运行控制台输出:欢迎使用本软件,第3次使用免费~
第四次及之后运行控制台输出:本软件只能免费使用3次,欢迎您注册会员后继续使用~
 */
public class BufferedDemo05 {
    public static void main(String[] args) throws IOException {
        //选择合适的流--BufferedReader中的readLine()方法可以一次读取一行数据
        BufferedReader br = new BufferedReader(new FileReader(""));
        int lineInt = Integer.parseInt(br.readLine());
        br.close();
        lineInt++;

        if(lineInt<4){
            System.out.println("欢迎使用本软件,这是第"+lineInt+"次使用。");
        }else {
            System.out.println("本软件只能免费使用3次,欢迎您注册后再使用");
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter(""));
        bw.write(lineInt+"");//这里要加+""保证写入文件中的是字符串,而不是数字(数字会读成ASCII码)
        bw.close();
    }
}

三、字符转换流

1、字节流和字符流之间的桥梁
2、字节流可以使用字符流中的方法

  • InputStreamReader(转换输入流)(高级流)
  • OutputStreamWriter(转换输出流)(高级流)
import java.io.*;
import java.nio.charset.Charset;
//利用转换流按照字符编码读取文件中数据
public class ConvertDemo01 {
    public static void main(String[] args) throws IOException {
        //jdk11之前
        InputStreamReader ir = new InputStreamReader(new FileInputStream(""), "GBK");
        int ch;
        while ((ch=ir.read())!=-1){
            System.out.println((char) ch);
        }
        ir.close();

        //jdk11之后
        FileReader fr = new FileReader("", Charset.forName("GBK"));
        int ch2 = 0;
        while (fr.read()!=-1){
            System.out.println((char)ch2);
        }
        fr.close();
    }
}
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
//利用转换流按照字符编码写出数据
public class ConvertDemo02 {
    public static void main(String[] args) throws IOException {
        //已淘汰
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(""), "GBK");
        osw.write("hsdj");
        osw.close();
        //现在--Charset.forName()
        FileWriter fw = new FileWriter("", Charset.forName("GBK"));
        fw.write("hdjf");
        fw.close();
    }
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
//将GBK编码的文件,转成UTF-8
//思路:用GBK编码读取文件,UTF-8写入文件
public class ConvertDemo03 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("", Charset.forName("GBK"));
        FileWriter fw = new FileWriter("", StandardCharsets.UTF_8);
        int ch;
        while ((ch=fr.read())!=-1){
            fw.write(ch);
        }
        fw.close();
        fr.close();
    }
}
import java.io.*;
//需求:用字节流读取文件中的数据(中文),每次读取一行,且不会出现乱码
public class ConvertDemo04 {
    public static void main(String[] args) throws IOException {
        //层层包装--JDK11之前
        FileInputStream fis = new FileInputStream("");//字节流--乱码
        InputStreamReader isr = new InputStreamReader(fis);//用字符转换流包装字节流--解决乱码
        BufferedReader br = new BufferedReader(isr);//再次用缓冲流包装--实现每次读取一行数据
        String str = br.readLine();
        System.out.println(str);
        br.close();

        //简化写法--JDK11之后
        BufferedReader br2 = new BufferedReader(new InputStreamReader(new FileInputStream("")));
        String strLine;
        while ((strLine=br2.readLine())!=null){
            System.out.println(strLine);
        }
        br2.close();
    }
}
posted @ 2023-04-11 22:49  小园初来乍到  阅读(31)  评论(0)    收藏  举报