javaSE基础知识——day16 IO流:字符流OutputStreamWriter、InputStreamReader,FileWriter和FileReader以及高效字节流BufferRead

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

A: 案例演示: 字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流。
B: 码表
C:字符流: 字符流 = 字节流 + 编码表

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。

使用什么字符集进行编码,那么就是使用什么字符集进行解码

老地方 ----- 十进制 ---- 二进制 ---- 发出去

接收 ---- 二进制 ---- 十进制 — 老地方

A:案例演示
String类中的编码和解码问题
默认的字符集是GBK的
String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组
byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组

编码:把看得懂的变成看不懂的: String – byte[]
解码:把看不懂的变成看得懂的: byte[] – String

案例:

import java.io.*;

public class MyTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //字节流:可以读写任意类型的文件
        // FileInputStream
        //  FileOutputStream
        //  BufferedOutputStream
        //  BufferedInputStream
        //字符流:只能读写文本文件

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

        //编码和解码
        //编码:把字符串转换成字节数组,把看的懂的转换成看不懂的
        //解码:把字节数组转换成字符串,把看不懂的,转换成看的懂
        //乱码:编解码采用的码表不一致,就会导致乱码

        //byte[] getBytes ()
        //使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
        //byte[] getBytes (Charset charset)
        //使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。


        String str = "你好中国";
        byte[] bytes = str.getBytes("UTF-8"); //编码

        //String( byte[] bytes)
        //通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
        //String( byte[] bytes, Charset charset)
        //通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。

        String s = new String(bytes, "UTF-8");//解码
        System.out.println(s);

        //发电报--原理  发的是二进制 电流强 0 弱 1  010101010101----十进制------

        //延安 密码本 ---上海 密码本

    }
}


转换流OutputStreamWriter的使用

A:OutputStreamWriter的构造方法
OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
B:案例演示: OutputStreamWriter写出数据

案例:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;


public class MyTest3 {
    public static void main(String[] args) throws IOException {


        //OutputStreamWriter(OutputStream out, Charset cs)
        //创建使用给定字符集的 OutputStreamWriter。
        //OutputStreamWriter(OutputStream out, String charsetName)
        //创建使用指定字符集的 OutputStreamWriter。
        //参数2:可以指定码表
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt",true), "UTF-8");
        out.write("指定了一个编码方式");
        out.write("\r\n"); //写入换行符
        out.write("这个是选择性的截取字符一部分",0,8);//写字符串的一部分,从 0开始,写8个字符
        out.flush();
        out.close();
    }
}


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

A: 方法概述
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) 写一个字符串的一部分

B:案例演示: 字符流的5种写数据的方式

案例一:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class MyTest4 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("b.txt"));
        out.write(new char[]{'a', '你', '好', '漂', '亮'});
        out.write("\r\n");
        out.write(new char[]{'a', '你', '好', '漂', '亮'}, 1, 4);//写入字符数组的一部分,从1开始写4个字符
        out.flush();
        out.close();

    }
}


转换流InputStreamReader的使用

A:InputStreamReader的构造方法
InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
B:案例演示: InputStreamReader读取数据

案例:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


public class MyTest {
    public static void main(String[] args) throws IOException {
        //InputStreamReader 是字节流通向字符流的桥梁:
        //它使用指定的 charset 读取字节并将其解码为字符。
        //它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
       // InputStreamReader
       // InputStreamReader(InputStream in)
       // 创建一个使用默认字符集的 InputStreamReader。
       // InputStreamReader(InputStream in, Charset cs)
       // 创建使用给定字符集的 InputStreamReader。
        //输入流,所关联的文件如果不存在就会报错
        InputStreamReader in = new InputStreamReader(new FileInputStream("c.txt"));
        int len = in.read(); //一次读取一个字符
        System.out.println(len);
        len = in.read(); //一次读取一个字符
        System.out.println(len);
        len = in.read(); //一次读取一个字符
        System.out.println(len);
        len = in.read(); //一次读取一个字符
        System.out.println(len);
        len = in.read(); //一次读取一个字符
        System.out.println(len);
        len = in.read(); //一次读取一个字符
        System.out.println(len);
        len = in.read(); //一次读取一个字符
        System.out.println(len); //如果读取不到有效字符,返回 -1 我们就会拿-1 判断这个文件是否读完

        in.close();
    }
}


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

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

案例:

import java.io.*;

public class MyTest {
    public static void main(String[] args) throws IOException {
        //我们用字符流来复制文本文件
        //一次读取一个字符,写入一个字符来复制
        InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\MyTest.java"));
        //定义一个变量,记录每次读取到的字符
        int len = 0;
        while ((len = in.read()) != -1) {
            out.write(len);
            out.flush();
        }
        in.close();
        out.close();
    }
}


案例二:

import java.io.*;


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("C:\\Users\\Administrator\\Desktop\\MyTest.java"));
        //定义一个字符缓冲区
        char[] chars = new char[2000];
        int len = 0;//记录每次读取到的有效字符个数
        while ((len = in.read(chars)) != -1) {
            out.write(chars, 0, len);
            out.flush();
        }

        in.close();
        out.close();
    }
}


IO流便捷类(FileWriter和FileReader复制文本文件)

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

字符流便捷类: 因为转换流的名字太长了,并且在一般情况下我们不需要制定字符集,
于是java就给我们提供转换流对应的便捷类

转换流 便捷类
OutputStreamWriter ------- FileWriter
InputStreamReader ------- FileReader
案例一:

import javax.annotation.processing.FilerException;
import java.io.*;


public class MyTest {
    public static void main(String[] args) throws IOException {

        // 父类                  子类 便捷类 不能指定码表,用的是默认码表
       // InputStreamReader---- FileReader
      //  OutputStreamWriter---- FileWriter

        FileReader reader = new FileReader("MyTest.java");
        FileWriter writer = new FileWriter("MyTest134.java");
        char[] chars = new char[1000];
        int len=0;
        while ((len=reader.read(chars))!=-1){
            writer.write(chars,0,len);
            writer.flush();
        }
        reader.close();
        writer.close();
    }
}


案例二:

import java.io.*;

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        //高效的字符流 里面有特有的方法比较好用
        // BufferedReader
        //  BufferedWriter

        BufferedReader bfr = new BufferedReader(new FileReader("MyTest134.java"));
        BufferedWriter bfw = new BufferedWriter(new FileWriter("MyTest1344444.java"));
        char[] chars = new char[1000];
        int len = 0;
        while ((len = bfr.read(chars)) != -1) {
            bfw.write(chars, 0, len);
            bfw.flush();
        }
        bfr.close();
        bfw.close();

    }
}


字符缓冲流的基本使用

A:案例演示: BufferedWriter写出数据 高效的字符输出流
B:案例演示: BufferedReader读取数据 高效的字符输入流

高效的字符流
高效的字符输出流: BufferedWriter
构造方法: public BufferedWriter(Writer w)
高效的字符输入流: BufferedReader
构造方法: public BufferedReader(Reader e)

案例:

import java.io.*;


public class MyTest3 {
    public static void main(String[] args) throws IOException {
        //高效的字符流 里面有特有的方法比较好用
        // BufferedReader   String readLine() 一次读取一个文本行。
        //  BufferedWriter   void newLine()写入一个换行符 具有平台兼容性
        //一次读取一行,写入一行,来复制文本文件
        BufferedReader bfr = new BufferedReader(new FileReader("MyTest134.java"));
        BufferedWriter bfw = new BufferedWriter(new FileWriter("MyTest555.java"));
        String line = null;
        while ((line = bfr.readLine()) != null) { //注意返回值是 读取不到返回null
            bfw.write(line);
            bfw.newLine();
            bfw.flush();
        }
        bfr.close();
        bfw.close();
    }
}


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

A:案例演示: 需求:把ArrayList集合中的字符串数据存储到文本文件

a: 创建一个ArrayList集合
b: 添加元素
c: 创建一个高效的字符输出流对象
d: 遍历集合,获取每一个元素,把这个元素通过高效的输出流写到文本文件中
e: 释放资源
案例一:
//A:
//案例演示:
//需求:把ArrayList集合中的字符串数据存储到文本文件
//分析:

package javaSEreview20190724.字符流和字节流的练习题;

import java.io.*;
import java.util.ArrayList;

/**
 * @Description:TODO
 * @Author:@李小白
 * @Date:2019/7/24 21:40
 */
public class Demo03把集合中的数据存储到文本文档中 {
    public static void main(String[] args) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        list.add("把集合");
        list.add("中的");
        list.add("数据");
        list.add("添加到");
        list.add("文本文档中");
        //两种方法都可以
//        FileWriter fw = new FileWriter("a.txt");
        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
        for (String s : list) {
            bw.write(s);
            bw.newLine();
//            fw.write(s);
//            fw.write("\r\t");
//            fw.flush();
            bw.flush();
        }
//        fw.close();
        bw.close();
    }
}

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

A:案例演示: 需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
分析:

  • a: 创建高效的字符输入流对象
  • b: 创建一个集合对象
  • c: 读取数据(一次读取一行)
  • d: 把读取到的数据添加到集合中
  • e: 遍历集合
  • f: 释放资源

案例一:

package javaSEreview20190724.字符流和字节流的练习题;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Description:TODO
 * @Author:@李小白
 * @Date:2019/7/24 21:59
 */
public class Demo04文本文档中的读取到集合中 {
    public static void main(String[] args) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        /*//方式一,我选择方式一
        String len=null;
        while ((len=br.readLine())!=null) {
            list.add(len);
        }
         br.close();
        for (String s : list) {
            System.out.println(s);
        }*/
        //方式二
        while (true){
            String s = br.readLine();
            if(s!=null){
                list.add(s);
            }
            break;
        }
    }
}

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

A:案例演示: 需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
a: 创建一个高效的字符输入流对象
b: 创建集合对象
c: 读取数据,把数据存储到集合中
d: 产生一个随机数,这个随机数的范围是 0 - 集合的长度 . 作为: 集合的随机索引
e: 根据索引获取指定的元素
f: 输出
g: 释放资源

方法一:

package javaSEreview20190724.字符流和字节流的练习题;

import org.omg.PortableServer.LIFESPAN_POLICY_ID;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

/**
 * @Description:TODO
 * @Author:@李小白
 * @Date:2019/7/24 22:11
 */
public class Demo05随机获取文本文件中的姓名 {
    public static void main(String[] args) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        list.add("随机");
        list.add("获取");
        list.add("文本文件");
        list.add("中的");
        list.add("学生");
        list.add("姓名");
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        String len=null;
        while ((len=br.readLine())!=null){
            list.add(len);
        }
        Random random = new Random();//生成随机数
        int index = random.nextInt(list.size());//随机生成的下一个数字以及总长度
        String s = list.get(index);//对集合索引进行输出
        System.out.println(s);

    }
}

方法二:

package org.westos.demo2;

import java.io.*;
import java.util.Scanner;

/**
 * @Author: ShenMouMou
 * @CreateTime: 2019-07-24 09:54
 * @Description:西部开源教育科技有限公司
 */
public class MyTest4 {
    public static void main(String[] args) throws FileNotFoundException {
        //InputStream in = System.in; //in 关联的是键盘
        //Scanner scanner = new Scanner(in);
        //Scanner(File source)
        //构造一个新的 Scanner,它生成的值是从指定文件扫描的。
        //
        //Scanner(InputStream source)
        //构造一个新的 Scanner,它生成的值是从指定的输入流扫描的。

        Scanner scanner = new Scanner(new File("username.txt"));
        while (scanner.hasNextLine()){
            String s = scanner.nextLine();
            System.out.println(s);
        }

        System.out.println("---------------------------");
        Scanner scanner2 = new Scanner(new FileInputStream("username.txt"));

        while (scanner2.hasNextLine()) {
            String s = scanner2.nextLine();
            System.out.println(s);
        }

    }
}

IO流 复制单级文件夹

A:案例演示: 需求: 复制D:\course这文件夹到E:\course

分析:
a: 封装D:\course为一个File对象
b: 封装E:\course为一个File对象,然后判断是否存在,如果不存在就是创建一个目录
c: 获取a中的File对应的路径下所有的文件对应的File数组
d: 遍历数组,获取每一个元素,进行复制
e: 释放资源
案例:

package javaSEreview20190724.字符流和字节流的练习题;

import java.io.*;

/**
 * @Description:单及文件的复制
 * @Author:@李小白
 * @Date:2019/7/24 22:29
 */
public class Demo06复制单击文件夹到其他路径 {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\demo");//源文件路径
        File newfile = new File("D:\\demo");//新文件路径
        if (!newfile.exists()) {
            newfile.mkdirs();//如果新文件不存在,就创建一个新的文件夹
        }
        File[] files = file.listFiles();//遍历旧的文件夹,准备开始单层复制
        for (File f : files) {
          //  System.out.println(file.getName());
           // System.out.println(f);
            getCopy(f,newfile);
        }

    }

    private static void getCopy(File f, File newfile) throws IOException {
        FileInputStream in = new FileInputStream(f);
//        System.out.println(newfile);
//       System.out.println(f.getName());
        FileOutputStream out = new FileOutputStream(new File(newfile, f.getName()));
        byte[] bytes = new byte[1024 * 8];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(len);
            out.flush();
        }
        in.close();
        out.close();
    }


}

复制多级文件

package javaSEreview20190724.字符流和字节流的练习题;

import java.io.*;

/**
 * @Description:单及文件的复制
 * @Author:@李小白
 * @Date:2019/7/24 22:29
 */
public class Demo06复制单击文件夹到其他路径 {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\demo");//源文件路径
        File newfile = new File("D:\\demo");//新文件路径
        if (!newfile.exists()) {
            newfile.mkdirs();//如果新文件不存在,就创建一个新的文件夹
        }
        getManyCopy(file, newfile);
    }

    private static void getManyCopy(File file, File newfile) throws IOException {
        File[] files = file.listFiles();//遍历旧的文件夹,准备开始单层复制
        for (File f : files) {
            if (f.isFile()){//如果是单层文件就调这个方法
                getCopy(f,newfile);
            }else {//如果是多级文件
                File file1 = new File(newfile,f.getName());
                if (!file1.exists()) {
                    file1.mkdirs();
                }
                getManyCopy(f,file1);
            }

        }
    }

    private static void getCopy(File f, File newfile) throws IOException {
        FileInputStream in = new FileInputStream(f);
//        System.out.println(newfile);
//       System.out.println(f.getName());
        FileOutputStream out = new FileOutputStream(new File(newfile, f.getName()));
        byte[] bytes = new byte[1024 * 8];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(len);
            out.flush();
        }
        in.close();
        out.close();
    }


}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值