java语言--------javaSE之字符流

字符流

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

A: 字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流。
B: 码表 UTF-8 GBK
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。

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

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

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

public static void main(String[] args) throws UnsupportedEncodingException {
    //字符流:只能对文本文件进行读写。
    //字符流=字节流+编码表

    //编码:把字符串转换为字节数组。
    //解码:把字节数组转换成字符串。
    //乱码:编解码没有采用同一张码表
    //getBytes(); 采用平台默认的码表进行编码。

      //getBytes("GBK"); 自己指定编码表
    byte[] bytes = "今天晚上学校小树林见".getBytes("UTF-8");

    for (byte aByte : bytes) {
        System.out.println(aByte);
    }

    System.out.println("=================================");
    //new String(bytes); 采用平台默认的码表进行解码。
    //String s = new String(bytes, "GBK"); 指定码表进行解码
    String s = new String(bytes,"GBK");
    System.out.println(s);

    // 发电报:  老地方见

    // 收报方:

}

转换流OutputStreamWriter的使用

  • OutputStreamWriter的构造方法
    OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
    OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
  • 方法概述
    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 static void main(String[] args) throws IOException {
    // OutputStreamWriter 是字符流通向字节流的桥梁:
    // 可使用指定的 码表 将要写入流中的字符编码成字节。
    // 它使用的字符集可以由名称指定或显式给定,
    // 否则将接受平台默认的字符集。
   // OutputStreamWriter(OutputStream out)
    //创建使用默认字符编码的 OutputStreamWriter。
    //输出流所关联的文件,如果不存在,会自动创建
    java.io.OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("a.txt",true));
    //一次写一个字符
    writer.write('a');
    writer.write('a');
    writer.flush();
    writer.write('a');
    writer.write('a');
    writer.flush();
    writer.write('a');
    writer.flush();
    writer.write('a');
    writer.write('a');
    writer.write('a');
    writer.write("家有小女初长成,养在深闺人未识,天生丽质难自弃,一朝选在君王侧,从此君王不早朝");
    writer.write("\r\n");
    writer.write(new char[]{'好','好','学','习'});
    writer.write("\r\n");
    writer.write("杨家有女初长成,养在深闺人未识,天生丽质难自弃,一朝选在君王侧,从此君王不早朝",0,7);
    writer.write("\r\n");
    writer.write(new char[]{'好', '好', '学', '习'},0,2);
    writer.flush(); //字符流记得刷新一下
    writer.close(); //刷新并关闭。
}
public static void main(String[] args) throws IOException {
  /*  OutputStreamWriter(OutputStream out, String charsetName)
    创建使用指定字符集的 OutputStreamWriter。*/
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("b.txt"), "UTF-8");
    writer.write("江畔何人初见月,江月何年初照人。");
    writer.flush();
    writer.close();

}

转换流InputStreamReader的使用

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

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

public static void main(String[] args) throws IOException {
   /* InputStreamReader 是字节流通向字符流的桥梁:
    它使用指定的 charset 读取字节并将其解码为字符。
    它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。*/

    /*InputStreamReader(InputStream in)
    创建一个使用默认字符集的 InputStreamReader。
    InputStreamReader(InputStream in, String charsetName)
    创建使用指定字符集的 InputStreamReader。*/
    //输入流,所关联的文件,如果不存在,就会报错。
    InputStreamReader reader = new InputStreamReader(new FileInputStream("c.txt"));
    int ch = reader.read();
    System.out.println(ch);
    ch = reader.read();
    System.out.println(ch);
    ch = reader.read();
    System.out.println(ch);
    ch = reader.read();
    System.out.println(ch);
    ch = reader.read();
    System.out.println(ch);
    ch = reader.read();
    System.out.println(ch);
    ch = reader.read();
    System.out.println(ch);
    //如果读取不到有效字符,返回 -1 我们使用 -1 来判断文件是否读取完。
    ch = reader.read();
    System.out.println(ch);
}
public static void main(String[] args) throws IOException {
    InputStreamReader reader = new InputStreamReader(new FileInputStream("c.txt"));
    char[] chars = new char[1000];
    //一次读取一个字符数组,返回值,返回的是读取到的有效的字符个数
      // int len = reader.read(chars);
    int len = reader.read(chars,0,3);
    System.out.println(len);
    for (char aChar : chars) {
        System.out.println(aChar);
    }
    reader.close();
}

字符流复制文本文件

public static void main(String[] args) throws IOException {
    //一次读取一个字符,写一个字符来复制文本文件
    InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
    OutputStreamWriter out= new OutputStreamWriter(new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\MyTest.java"));

    int ch=0;
    while ((ch=in.read())!=-1){
        out.write(ch);
        out.flush();
    }

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

}
public static void main(String[] args) throws IOException {
    //一次读写一个字符数组,来复制
    //一次读取一个字符,写一个字符来复制文本文件
    InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\MyTest.java"));

    char[] chars = new char[1000];
    int len=0;
    while ((len=in.read(chars))!=-1){
        // System.out.println("循环次数");
        out.write(chars,0,len);
        out.flush();
    }
    in.close();
    out.close();

}

FileWriter和FileReader复制文本文件

public static void main(String[] args)  {
   // new FileReader()
   //  new FileWriter()
   //  父类---------------------子类
  //  InputStreamReader------ FileReader
  //  OutputStreamWriter-------- FileWriter
    FileReader in=null;
    FileWriter out=null;

    try {
        in = new FileReader("C:\\Users\\ShenMouMou\\Desktop\\MyTest.java");
         out = new FileWriter("MyTest.java");

        char[] chars = new char[1000];
        int len = 0;
        while ((len = in.read(chars)) != -1) {
            // System.out.println("循环次数");
            out.write(chars, 0, len);
            out.flush();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

字符缓冲流的基本使用

  • 高效的字符流

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

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

字符缓冲流复制文本文件

public static void main(String[] args) throws IOException {
   // BufferedReader
    //ctrl+H 可以看一个类的继承关系。
  /*  BufferedWriter(Writer out)
    创建一个使用默认大小输出缓冲区的缓冲字符输出流。*/
    BufferedWriter out = new BufferedWriter(new FileWriter("d.txt"));
   // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e.txt")));

    BufferedReader  in= new BufferedReader(new FileReader("MyTest.java"));

    char[] chars = new char[1000];
    int len = 0;
    while ((len = in.read(chars)) != -1) {
        // System.out.println("循环次数");
        out.write(chars, 0, len);
        out.flush();
    }

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

}

字符缓冲流的特殊功能

  • 字符缓冲流的特殊功能
    BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符
    BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数据返回null
    包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null

字符缓冲流的特殊功能复制文本文件

public static void main(String[] args) throws IOException {
    //高效的字符流,特有的方法
  /*  void newLine ()
    写入一个行分隔符。*/

   /* String readLine ()
    读取一个文本行。*/

    BufferedWriter out = new BufferedWriter(new FileWriter("ff.txt"));
    // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e.txt")));

    BufferedReader in = new BufferedReader(new FileReader("MyTest.java"));

    //读取一行,写入一行,来复制文件
    String line=null;
    while ((line=in.readLine())!=null){
        out.write(line);
        out.newLine();
        out.flush();
    }

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

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

需求:把ArrayList集合中的字符串数据存储到文本文件
分析:

  • a: 创建一个ArrayList集合
  • b: 添加元素
  • c: 创建一个高效的字符输出流对象
  • d: 遍历集合,获取每一个元素,把这个元素通过高效的输出流写到文本文件中
  • e: 释放资源
public static void main(String[] args) throws IOException {
   /* A:
    案例演示:
    需求:把ArrayList集合中的字符串数据存储到文本文件*/
    ArrayList<String> list = new ArrayList<>();
    list.add("贾宝玉");
    list.add("林黛玉");
    list.add("袭人");
    list.add("晴雯");

    BufferedWriter writer = new BufferedWriter(new FileWriter("hong.txt"));
    for (String name : list) {
        writer.write(name);
        writer.newLine();
        writer.flush();
    }
    writer.close();
}

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

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

​ a: 创建高效的字符输入流对象

​ b: 创建一个集合对象

​ c: 读取数据(一次读取一行)

​ d: 把读取到的数据添加到集合中

​ e: 遍历集合

​ f: 释放资源

BufferedReader reader = new BufferedReader(new FileReader("hong.txt"));
ArrayList<String> list = new ArrayList<>();
while (true){
    String name = reader.readLine();
    if (name != null) {
        list.add(name);
    }else{
        break;
    }
}

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

需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称

​ 分析:

​ a: 创建一个高效的字符输入流对象

​ b: 创建集合对象

​ c: 读取数据,把数据存储到集合中

​ d: 产生一个随机数,这个随机数的范围是 0 - 集合的长度 . 作为: 集合的随机索引

​ e: 根据索引获取指定的元素

​ f: 输出

​ g: 释放资源

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("hong.txt"));
    ArrayList<String> list = new ArrayList<>();
    while (true){
        String name = reader.readLine();
        if (name != null) {
            list.add(name);
        }else{
            break;
        }

    }
   // System.out.println(list);
    //随机取一个人
    Random random = new Random();
    int index = random.nextInt(list.size());
    String s = list.get(index);
    System.out.println(s);
    //这种设计,是要把数据和程序解耦

}

案例演示:

  • 删除多级文件夹

        public static void main(String[] args) {
            //删除多级文件夹
            //1.封装源文件夹
            File srcFolder = new File("C:\\Users\\ShenMouMou\\Desktop\\demo");
    
            deleteFolder(srcFolder);
        }
    
        private static void deleteFolder(File srcFolder) {
            File[] files = srcFolder.listFiles();
            for (File f : files) {
                if (f.isFile()) {
                    f.delete();
                }else{
                    deleteFolder(f);
                }
            }
            srcFolder.delete();
        }
  • 复制多级文件夹,如果文件夹里有.jpg 结尾的文件,把.jpg 改为.png

public class Test {
    public static void main(String[] args) throws IOException {
        File scrFile = new File("C:\\Users\\pc\\Desktop\\aa");
        File deskFile = new File("E:\\aa-copy");
        copyFolder(scrFile,deskFile);

        File xgfile = new File("E:\\aa-copy");
        xgfile(xgfile);
    }
	//修改文件后缀名
    private static void xgfile(File xgfile) {
        if(xgfile.isDirectory()){
            File[] files = xgfile.listFiles();
            for (File file : files) {
                if(file.isFile()&&file.getName().endsWith(".jpg")){
                    String oldPath = file.getAbsolutePath();
                    String substring = oldPath.substring(0, oldPath.lastIndexOf("."));
                    File file1 = new File(substring+".png");
                    file.renameTo(file1);
                }else {
                    xgfile(file);
                }
            }
        }
    }

    private static void copyFolder(File scrFile, File deskFile) throws IOException {
        if(scrFile.isDirectory()){
            File newFilder = new File(deskFile, scrFile.getName());
            newFilder.mkdirs();
            File[] files = scrFile.listFiles();
            for (File file : files) {
                copyFolder(file,newFilder);
            }
        }else {
            File newFile = new File(deskFile, scrFile.getName());
            copyFile(scrFile,newFile);
        }
    }
 			// 复制文件
    private static void copyFile(File scrFile, File newFile) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(scrFile));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));
        byte[] bytes = new byte[1024];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
        in.close();
        out.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值