Java-IO流(字符流)、String中的编解码问题、转化流

在这里插入图片描述

字符流出现的原因及编码表概述和常见编码表

  • 字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流。
  • 码表:就是把字符串转换成字节数组
  • 字符流: 字符流 = 字节流 + 编码表
  • 常见编码表:UTF-8、GBK、UTF-16

String类中的编码和解码问题

编码: 就是把字符串转换成字节数组

- 把一个字符串转换成一个字节数组

- public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 
- public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 
- 解码: 把字节数组转换成字符串
- public String(byte[] bytes):	通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
- public String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String
- 使用什么字符集进行编码,那么就是使用什么字符集进行解码
- 老地方  ----- 十进制   ---- 二进制  ---- 发出去
- 接收 ---- 二进制  ---- 十进制  --- 老地方
tring(byte[] bytes, String charsetName):通过指定的字符集解码字节数组
byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组
编码:把看得懂的变成看不懂的:	String -- byte[]
解码:把看不懂的变成看得懂的:	byte[] -- String

转换流OutputStreamWriter的使用

  • OutputStreamWriter的构造方法
    OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
    OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
  • 案例演示(单字节获取数据):
public class MyTest2 {
    public static void main(String[] args) throws IOException {
        //采用字符流,来复制,文本文件
        InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
        //读一个字符,写一个字符来复制
        int len=0;//用来记录你读取到的那个字符
        while ((len=in.read())!=-1){
            out.write(len);
            out.flush();//字符流记得刷新一下
        }
        in.close();
        out.close(); //关闭并刷新
    }
}

字符流的5种写数据的方式

  • 方法概述
    public void write(int c) 写一个字符
    public void write(char[] cbuf) 写一个字符数组
    public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分
    public void write(String str) 写一个字符串
    public void write(String str,int off,int len) 写一个字符串的一部分

转换流InputStreamReader的使用

  • InputStreamReader的构造方法
    InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
    InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据

字符流的2种读数据的方式

  • 方法概述
    public int read() 一次读取一个字符
    public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1

字符流复制文本文件(用数组获取数据)

public class MyTest3 {
    public static void main(String[] args) throws IOException {
        //一次读取一个字符数组,写出一个字符数组 推荐 使用
        InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
        char[] chars = new char[1000];
        int len=0;//读取到有效字符个数
        while ((len=in.read(chars))!=-1){
           out.write(chars,0,len);
           out.flush();//字符流记得刷新一下
        }
        in.close();
        out.close();
    }
}

FileWriter和FileReader复制文本文件

  • FileReader和FileWriter的出现
    转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,
    所以,为了简化我们的书写,转换流提供了对应的子类。
    FileWriter
    FileReader
  • 案例演示: FileWriter和FileReader复制文本文件
public class MyTest {
    public static void main(String[] args) throws IOException {
        //字符转换流
        //InputStreamReader 字节流到字符流桥梁
        //OutputStreamWriter 字符流到字节流的桥梁
        转换流  父类     子类 便捷流 唯一缺点,就是不能指定编码,他也没有特有方法,都是从父类继承下来的
        //InputStreamReader----- FileReader
        //OutputStreamWriter------ FileWriter
        FileReader in = new FileReader("src/MyTest.java");
        FileWriter out = new FileWriter("MyTest3.java");
        char[] chars=new char[1000];
        int len=0;
        while ((len=in.read(chars))!=-1){
            //System.out.println("读取的次数"+len);
            out.write(chars,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }
}
  • 字符流便捷类: 因为转换流的名字太长了,并且在一般情况下我们不需要制定字符集,于是java就给我们提供转换流对应的便捷流
    转换流----------------------------便捷类
    OutputStreamWriter ------- FileWriter
    InputStreamReader ------- FileReader

字符缓冲流的基本使用

  • 高效的字符流
    高效的字符输出流: BufferedWriter
    构造方法: public BufferedWriter(Writer w)
    高效的字符输入流: BufferedReader
    构造方法: public BufferedReader(Reader e)
  • 案例演示:
public class MyTest {
    public static void main(String[] args) throws IOException {
        //高效的字符流
        //BufferedReader
        // BufferedWriter
        // BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
        // 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
        BufferedReader bfr = new BufferedReader(new FileReader("a.txt"));
        BufferedWriter bfw = new BufferedWriter(new FileWriter("aa.txt"));
        char[] chars = new char[1024];
        int len = 0;
        while ((len=bfr.read(chars)) != -1) {
            bfw.write(chars, 0, len);
            bfw.flush();
        }
        bfr.close();
        bfw.close();
    }
}

字符缓冲流的特殊功能

  • 字符缓冲流的特殊功能
    BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符
    BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数据返回null,包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
  • 案例演示: 字符缓冲流的特殊功能
public class MyTest1 {     
    public static void main(String[] args) throws IOException {         
        // BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
        // 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
        BufferedReader bfr = new BufferedReader(new FileReader("回顾.java"));
        BufferedWriter bfw = new BufferedWriter(new FileWriter("aaa.java"));
        //BufferedReader 他有一个特有的方法,readLine() 一次读取一行内容
        //bfr.readLine()
       // bfw.write("\r\n");
        //bfw.newLine(); //写一个换行符
        //读取一行,写出一行来复制
        String line=null;
        while ((line=bfr.readLine())!=null){
            bfw.write(line);
            bfw.newLine(); //写出一个换行符,具有平台兼容性
            bfw.flush();
        }        
        bfr.close();
        bfw.close();    
    }
}

把集合中的数据存储到文本文件

  • 案例演示: 需求:把ArrayList集合中的字符串数据存储到文本文件
    分析:
    • a: 创建一个ArrayList集合
    • b: 添加元素
    • c: 创建一个高效的字符输出流对象
    • d: 遍历集合,获取每一个元素,把这个元素通过高效的输出流写到文本文件中
    • e: 释放资源
public class MyTest {
    public static void main(String[] args) throws IOException {
       ArrayList<String> list = new ArrayList<>();
        list.add("张飞");
        list.add("赵云");
        list.add("马超");
        list.add("黄忠");
        list.add("关羽");
        FileWriter writer = new FileWriter("username.txt");
        for (String s : list) {
            writer.write(s);
            writer.write("\r\n");
            writer.flush();
        }
        writer.close();
    }
}

把文本文件中的数据存储到集合中

  • 案例演示: 需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
    分析:
    • a: 创建高效的字符输入流对象
    • b: 创建一个集合对象
    • c: 读取数据(一次读取一行)
    • d: 把读取到的数据添加到集合中
    • e: 遍历集合
    • f: 释放资源
public class MyTest2 {
    public static void main(String[] args) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        BufferedReader bfr = new BufferedReader(new FileReader("username.txt"));
        String line = null;
        while ((line = bfr.readLine()) != null) {
            list.add(line);
        }
        for (String s : list) {
            System.out.println(s);
        }
    }
}

随机获取文本文件中的姓名

  • 案例演示: 需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
    • a: 创建一个高效的字符输入流对象
    • b: 创建集合对象
    • c: 读取数据,把数据存储到集合中
    • d: 产生一个随机数,这个随机数的范围是 0 - 集合的长度 . 作为: 集合的随机索引
    • e: 根据索引获取指定的元素
    • f: 输出
    • g: 释放资源
public class MyTest3 {
    public static void main(String[] args) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("username.txt")));
        //读取一行数据,往集合里面添加一个
        String line=null;
        while ((line=reader.readLine())!=null){
            list.add(line);
        }
        //随机从集合里面抽取一个人
        Random random = new Random();
        int index = random.nextInt(list.size());
        String s = list.get(index);
        System.out.println(s);
    }
}

复制多级文件夹

  • 案例演示: 需求: 复制D:\course这文件夹到E:\course
    分析:
    • a: 封装D:\测试图片 为一个File对象
    • b: 封装E:\测试图片 为一个File对象,然后判断是否存在,如果不存在就是创建一个目录
    • c: 获取a中的File对应的路径下所有的文件对应的File数组
    • d: 遍历数组,获取每一个元素,进行复制
    • e: 释放资源
public class Demo {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\测试图片");
        File file1 = new File(file.getAbsolutePath().replace("D", "E"));
        if(!file1.exists()){
            file1.mkdirs();
        }
        copyFolder(file,file1);
    }

    private static void copyFolder(File file, File file1) throws IOException {
        File[] files = file.listFiles();
        for (File f : files) {
            if(f.isFile()){
                copyFile(f,file1);
            }else {
                file=new File(f.getAbsolutePath());
                file1=new File(file.getAbsolutePath().replace("D","E"));
                if(!file1.exists()){
                    file1.mkdirs();
                }
                copyFolder(file,file1);
            }
        }
    }

    private static void copyFile(File f, File file1) throws IOException {
        f = new File(f.getAbsolutePath());
        file1 = new File(f.getAbsolutePath().replace("D","E"));
        FileInputStream in = new FileInputStream(f);
        FileOutputStream out = new FileOutputStream(file1);
        byte[] bytes = new byte[1024 * 8];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
        in.close();
        out.close();
    }
}

键盘录入学生信息按照总分排序并写入文本文件

  • 案例演示: 需求:键盘录入3个学生信息(姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),按照总分从高到低存入文本文件
    分析:
    • a: 创建一个学生类: 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)
    • b: 因为要排序,所以需要选择TreeSet进行存储学生对象
    • c: 键盘录入学生信息,把学生信息封装成一个学生对象,在把学生对象添加到集合中
    • d: 创建一个高效的字符输出流对象
    • e: 遍历集合,把学生的信息写入到指定的文本文件中
    • f: 释放资源
public class Student {
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getChineseScore() {
        return chineseScore;
    }
    public void setChineseScore(int chineseScore) {
        this.chineseScore = chineseScore;
    }
    public int getMathScore() {
        return mathScore;
    }
    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }
    public int getEnglishScore() {
        return englishScore;
    }
    public void setEnglishScore(int englishScore) {
        this.englishScore = englishScore;
    }
    //获取总分的方法
    public  int totalScore(){
        return this.chineseScore+this.mathScore+this.englishScore;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", chineseScore=" + chineseScore +
                ", mathScore=" + mathScore +
                ", englishScore=" + englishScore +
                '}';
    }
}
--------------------------------------------------------------------------------------------
public class MyTest {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //按照总分排大小
              int num= s1.totalScore()-s2.totalScore();
              //再比较姓名
                int num2=num==0?s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });
        for (int i = 1; i <= 3; i++) {
            Student student = new Student();
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入"+i+"个学生的姓名");
            String name = scanner.nextLine();
            student.setName(name);
            System.out.println("请输入" + i + "个学生的语文成绩");
            int yw = scanner.nextInt();
            student.setChineseScore(yw);
            System.out.println("请输入" + i + "个学生的数学成绩");
            int xs= scanner.nextInt();
            student.setMathScore(xs);
            System.out.println("请输入" + i + "个学生的英语成绩");
            int yy = scanner.nextInt();
            student.setEnglishScore(yy);
            //把学生添加到集合里面去
            treeSet.add(student);
        }
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Student.txt",true)));
        //收到写一样表头
        writer.write("编号"+"\t"+"姓名"+"\t"+"语文"+"\t"+"数学"+"\t"+"外语"+"\t"+"总分");
        writer.newLine();
        writer.flush();
        //遍历集合把学生的数据存到文本文件中
        int index=1;
        for (Student student : treeSet) {
            writer.write(index+"\t"+student.getName()+"\t"+student.getChineseScore()+"\t"+student.getMathScore()+"\t"+student.getEnglishScore()+"\t"+student.totalScore());
            writer.newLine();
            writer.flush();
            index++;
        }
        writer.close();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值