字符流

字符流

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LGMrLdla-1590981059397)(D:\develop\学习\笔记 资料\IO流\20200525-JavaSE-课件\20200525-JavaSE-课件\字符流的继承关系.png)]

字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流。
一个汉字占三个字节(utf-8),如果使用字节流操作,需要对汉字进行拆分和并,比较麻烦。数据在计算机上存储的最小单位是字节,字符流底层还是使用字节流。使用字符流,就需要对汉字进行拆分合并,按照编码表。

计算机是如何识别把两个汉字转换为一个中文呢:
在计算机中,中文的存储分为两个字节(unicode),第一个字节肯定是负数,第二个常见的是负数,可以是正数

字符流 = 字节流 + 编码表

1.String类中的编码和解码问题

public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列 

String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组  
byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组

public class Test1 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //编码:按照某种编码方式把字符串转换成字节数组,把看的懂,变成你看不懂的
        //解码:把字节数组按照某种编码方式转成字符串,把看不懂的,变成看的懂

        String s="一二三";
        //public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列
        byte[] bytes = s.getBytes();
        System.out.println(Arrays.toString(bytes));
        //解码,public String(byte[] bytes):通过使用平台的默认字符集解码指定的 byte 数组
        String s1=new String(bytes);
        System.out.println(s1);
        //在编码和解码时使用的码表相同,就不会出现乱码问题

        String err="乱码问题";
        //按照GBK来编码
        byte[] gbks = err.getBytes("GBK");
        //按UTF-8解码
        String s2 = new String(gbks, "UTF-8");
        System.out.println(s2);

        //乱码出现的原因:就是编解码用的不是同一张码表
    }
}

OutputStreamWriter/InputStreamReader(转换流)

1,OutputStreamWriter

/* OutputStreamWriter 是字符流通向字节流的桥梁:
        可使用指定的 码表 将要写入流中的字符编码成字节。
        它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。*/
OutputStreamWriter是从字符流到字节流的桥接怎么理解?

     1、字符的输出需要通过字符流来操作,但是本质最后还是通过字节流输出到计算机上进行存储的

     2、 因此OutputStreamWriter流的作用就是利用字节流作为底层输出流然后构建字符输出流,字符输出流输出字符到流中,然后通过指定的字符集把流中的字符编码成字节输出到字节流中,其作用就是一个桥梁,使得双方链接起来

OutputStreamWriter的构造方法
	OutputStreamWriter(OutputStream out):根据默认编码把字节流的数据转换为字符流
	OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流

2,字符流写入数据的方式

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) 写一个字符串的一部分
	
演示:
public class Test1 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"));
        //写入数据
        //一次写入一个字符
        out.write('好');
        out.flush();//字符流,要刷新,把缓冲区中的数据,刷新到磁盘中
        //换行符
        out.write("\r\n");
        out.flush();
        out.write("好学习");
        out.flush();
        //一次写入一个字符串
        out.write("\r\n");
        out.write("一二三四");
        out.flush();
        //public void write(char[] cbuf) 写一个字符数组
        out.write(new char[]{'a','b','v','d'});
        //public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分
        out.write(new char[]{'a','b','v','d'},0,3);
        out.write("\r\n");
        //public void write(String str,int off,int len) 写一个字符串的一部分
        out.write("好好学习",2,2);
        //释放资源
        out.close();
    }
}
public class Test2 {
    public static void main(String[] args) throws IOException {
        //追加写入 在传入的字节流中,true表示追加写入
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("b.txt",true));
        out.write("hhhhhh");
        out.write("\r\n");
        out.write("好好好",0,2);
        out.write("\r\n");
        out.write(new char[]{'a','b','c'});
        out.write("\r\n");
        out.write(new char[]{'a','b','c'},0,2);
        out.write("\r\n");
        out.flush();
        out.close();
    }
}

3,InputStreamReader

/*InputStreamReader 是字节流通向字符流的桥梁:
        它使用指定的 码表 读取字节并将其解码为字符。
        它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集*/

InputStreamReader的构造方法
	InputStreamReader(InputStream is):用默认的编码读取数据
	InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
    
public class Test {
    public static void main(String[] args) throws IOException {
      //InputStreamReader(InputStream is):用默认的编码读取数据
        InputStreamReader in = new InputStreamReader(new FileInputStream("b.txt"));
        //InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
        InputStreamReader gbk = new InputStreamReader(new FileInputStream("b.txt"), "GBK");
    }
}    

4,字符流读取数据

public int read() 一次读取一个字符,如果没有读到 返回-1
public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1
    
public class Test1 {
    public static void main(String[] args) throws IOException {
        //字符流的2种读数据的方式

        InputStreamReader in = new InputStreamReader(new FileInputStream("b.txt"));
        // 一次读取一个字符
        int ch=in.read();
        System.out.println(ch);
        ch=in.read();
        System.out.println(ch);
        ch=in.read();
        System.out.println((char)ch);
        //如果读取不到,就返回-1

        in.close();
    }
}
public class Test2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("b.txt");
        out.write(new byte[]{'a','b','c','d'});
        //public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1
        InputStreamReader in = new InputStreamReader(new FileInputStream("b.txt"));
        //定义一个字符数组来充当容器,可以一次取到这个容器中
        char[] chars = new char[1000];
        int len = in.read(chars);
        System.out.println(len);
        System.out.println(Arrays.toString(chars));

        //把字符数组转换成字符串
        String ss=String.valueOf(chars);
        System.out.println(ss);
        String s = new String(chars);
        System.out.println(s);

        in.close();
    }
}
public class Test3 {
    public static void main(String[] args)  {
        InputStreamReader in=null;
        try{
             in = new InputStreamReader(new FileInputStream("b.txt"));
            //定义一个字符数组来充当容器
            char[] chars = new char[1000];
            //一次读取一部分字符,放到字符数组中
            int read = in.read(chars, 0, 2);
            System.out.println(read);
            System.out.println(Arrays.toString(chars));
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

5,字符流复制文本文件

public class Test {
    public static void main(String[] args) throws IOException {
        //使用字符流来复制文本文件
        //一次读写一个字符来复制
        InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("c.txt"));
        int ch=0;//用来记录每次读取到的字符
        while ((ch=in.read())!=-1){
            out.write(ch);
            out.flush();//字符流记得读取一些字符就刷新
        }
        in.close();
        out.close();
    }
}
public class Test1 {
    public static void main(String[] args) throws IOException {
        //字符流来复制文本文件
        //一次读写一个字符数组来复制
        InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d.txt"));
        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();
    }
}

FileReader/FileWriter 便捷字符流

FileReader和FileWriter的出现
	转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,
	所以,为了简化我们的书写,转换流提供了对应的子类。	
	FileWriter
	FileReader

public class Test {
    public static void main(String[] args) throws IOException {
        /*
        *   转换流	父类						便捷类 子类
         OutputStreamWriter	-------		    FileWriter
          InputStreamReader	-------		    FileReader

          便捷流不能指定编码,没有自己特有的方法
        * */

        /* FileReader(String fileName)
        在给定从中读取数据的文件名的情况下创建一个新 FileReader。
        FileReader(File file)
        在给定从中读取数据的 File 的情况下创建一个新 FileReader。*/

         /* FileWriter(File file)
        根据给定的 File 对象构造一个 FileWriter 对象。
        FileWriter(String fileName)
        根据给定的文件名构造一个 FileWriter 对象。
        FileWriter(String fileName, boolean append)
        根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。*/

       //便捷字符流来复制文件
        FileReader fileReader = new FileReader("c.txt");
        FileWriter fileWriter = new FileWriter("e.txt");

        char[] chars = new char[1000];
        int len=0;
        while ((len=fileReader.read(chars))!=-1){
            fileWriter.write(chars,0,len);
            fileWriter.flush();
        }
        fileReader.close();
        fileWriter.close();
    }
}

字符缓冲流

高效的字符流
	 高效的字符输出流:	BufferedWriter
	  		     构造方法:	public BufferedWriter(Writer w)
	 高效的字符输入流:	BufferedReader
	 		    构造方法:   public BufferedReader(Reader e)
                    
public class Test1 {
    public static void main(String[] args) throws IOException {
        //字符缓冲流
        //BufferedReader
        //BufferedWriter
      /*  构造方法摘要
        BufferedReader(Reader in)
        创建一个使用默认大小输入缓冲区的缓冲字符输入流。*/
        BufferedReader bufferedReader = new BufferedReader(new FileReader("e.txt"));
        //BufferedWriter(Writer out)
        //创建一个使用默认大小输出缓冲区的缓冲字符输出流。
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("f.txt"));
        int c=0;
        while ((c=bufferedReader.read())!=-1){
            bufferedWriter.write(c);
            bufferedWriter.flush();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
}                    

字符缓冲流的特殊功能

字符缓冲流的特殊功能
	BufferedWriter:	public void newLine():根据系统来决定换行符 具有系统兼容性的换行符
	BufferedReader:	public String readLine():一次读取一行数据  是以换行符为标记的 读到换行符就换行 没读到数据返回null
包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
        
public class Test2 {
    public static void main(String[] args) throws IOException {
        //高效的字符流特有的方法
        //public String readLine() 一次读一行文本
        //public void newLine():根据系统来决定换行符 具有系统兼容性的换行符

        //使用高效的字符流。读取一行,写入一行来复制文件

        BufferedReader in = new BufferedReader(new FileReader("Test1.java"));
        BufferedWriter out = new BufferedWriter(new FileWriter("copy.java"));

        String read=null;//定义一个变量,来记录每次读取到的一行文本
        while ((read=in.readLine())!=null){
            out.write(read);
            out.newLine();
            out.flush();
        }
        in.close();
        out.close();

    }
}        

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

public class Test {
    public static void main(String[] args) throws IOException {
        //把集合中的数据存储到文本文件
        //需求:把ArrayList集合中的字符串数据存储到文本文件
        /*
        * a: 创建一个ArrayList集合
          b: 添加元素
          c: 创建一个高效的字符输出流对象
          d: 遍历集合,获取每一个元素,把这个元素通过高效的输出流写到文本文件中
          e: 释放资源
        * */

        ArrayList<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("赵六");
        list.add("田七");

        BufferedWriter buf = new BufferedWriter(new FileWriter("jihe.txt"));
        //遍历集合
        for (String s : list) {
            buf.write(s);
            buf.newLine();
            buf.flush();
        }
        buf.close();
    }
}

从文本文件中读取数据(每一行为一个字符串数据) 到集合中,并遍历集合

public class Test1 {
    public static void main(String[] args) throws IOException {
        //需求:从文本文件中读取数据(每一行为一个字符串数据) 到集合中,并遍历集合
        BufferedReader in = new BufferedReader(new FileReader("jihe.txt"));
        ArrayList<String> list = new ArrayList<>();
        String read=null;
        while ((read=in.readLine())!=null){
            list.add(read);
        }
        for (String s : list) {
            System.out.println(s);
        }
    }
}

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

案例演示:	需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
 * 分析:
	 * 		a: 创建一个高效的字符输入流对象
	 * 		b: 创建集合对象
	 * 		c: 读取数据,把数据存储到集合中
	 * 		d: 产生一个随机数,这个随机数的范围是 0 - 集合的长度 . 作为: 集合的随机索引
	 * 		e: 根据索引获取指定的元素
	 * 		f: 输出
	 * 		g: 释放资源
 
public class Test2 {
    public static void main(String[] args) throws IOException {
        //需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
        ArrayList<String> list = new ArrayList<>();
        BufferedReader in = new BufferedReader(new FileReader("jihe.txt"));
        String name=null;
        while ((name=in.readLine())!=null){
            list.add(name);
        }
        in.close();

        //从集合中随机取一个人
        Random random = new Random();
        int index=random.nextInt(list.size());

        String get=list.get(index);
        System.out.println(get);

        //解耦:学生的名字是数据,你写的随机取人名是程序
        //我把代码和数据写在一块可以,但是不好,不利于维护。
        //把程序和数据分离开来,解耦,利用维护

    }
}         

复制指定目录下指定后缀名的文件并修改名称

public class Test1 {
    public static void main(String[] args) throws IOException {
        /*A:案例演示:	需求: 复制D:\\course这文件夹到E:\\course
- 分析:
- a: 封装D:\\course为一个File对象
- b: 封装E:\\course为一个File对象,然后判断是否存在,如果不存在就是创建一个目录
- c: 获取a中的File对应的路径下所有的文件对应的File数组
- d: 遍历数组,获取每一个元素,进行复制
- e: 释放资源
        *
        * */

        //1.封装源文件夹
        File srcForder = new File("D:\\develop\\学习\\笔记  资料\\IO流\\20200530-JavaSE-课件\\代码\\20200530-杂七杂八的流-上午");
        //2.封装目标文件夹  如果目标文件夹不存在,就创建出来
        File targertForder = new File("F:\\"+srcForder.getName());
        if (!targertForder.exists()){
            targertForder.mkdirs();
        }
        //复制的方法
        copyForder(srcForder,targertForder);
    }

    private static void copyForder(File srcForder, File targertForder) throws IOException {
        //遍历源文件夹,把源文件夹里面的文件,复制到目标文件夹
        File[] files = srcForder.listFiles();
        for (File file : files) {
            if (file.isFile()){
                //是文件,就复制
                copyFiles(targertForder,file);
            }else {
                //是子文件夹 递归调用
                File newForder = new File(targertForder, file.getName());
                if (!newForder.exists()){
                    newForder.mkdirs();
                }
                //递归
                copyForder(file,newForder);
            }
        }
    }

    private static void copyFiles(File targertForder, File file) throws IOException {
        FileInputStream in = new FileInputStream(file);
        //封装目标文件
        String name=file.getName();
        boolean flag=name.endsWith(".java");
        if (flag){
            name=name.substring(0,name.lastIndexOf("."))+".txt";

        }
        File mbFile = new File(targertForder, name);
        FileOutputStream out = new FileOutputStream(mbFile);
        byte[] bytes = new byte[1024 * 8];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }
}

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

public class Test {
    public static void main(String[] args) throws IOException {
        /*
        * 需求:键盘录入3个学生信息(姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),按照总分从高到低存入文本文件
- 分析:
- a: 创建一个学生类: 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)
- b: 因为要排序,所以需要选择TreeSet进行存储学生对象
- c: 键盘录入学生信息,把学生信息封装成一个学生对象,在把学生对象添加到集合中
- d: 创建一个高效的字符输出流对象
- e: 遍历集合,把学生的信息写入到指定的文本文件中
- f: 释放资源
        * */
        TreeSet<Student> students = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num=o1.getTotal()-o2.getTotal();
                int num2=(num==0)?o1.getName().compareTo(o2.getName()):num;
                return -num2;
            }
        });

        for (int i=1;i<=3;i++){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入第"+i+"个学生的姓名");
            String name = sc.nextLine();
            System.out.println("请输入第" + i + "个学生的语文成绩");
            int yw= sc.nextInt();
            System.out.println("请输入第" + i + "个学生的数学成绩");
            int sx = sc.nextInt();
            System.out.println("请输入第" + i + "个学生的英语成绩");
            int yy = sc.nextInt();
            //创建学生对象,封装每个学生的信息
            Student student = new Student(name, yw, sx, yy);
            students.add(student);
        }
        //增加一个需求:每一次录入成绩,都保存在一个新的文件中

        //遍历学生这个集合,把每个学生的信息,存储到文本文件中
        //写个表头 改成追加写入
        //用时间来命名这个文件名
        long l = System.currentTimeMillis();
        Date date=new Date(l);
        SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        String name=f.format(date);

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(name,true));
        bufferedWriter.write("名次\t姓名\t语文\t数学\t英语\t总分");
        bufferedWriter.newLine();
        bufferedWriter.flush();
        int i=0;
        for (Student student : students) {
            bufferedWriter.write((++i)+"\t"+student.getName()+"\t"+student.getChineseScore()+"\t"+student.getMathScore()+"\t"+student.getEnglishScore()+"\t"+student.getTotal());
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
        bufferedWriter.close();
        System.out.println("存储完成");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值