zoukankan      html  css  js  c++  java
  • 字符流的父类

    reader 字符输入流

    • public int read(){}
    • public int read(char[] c){}
    • public int read(char[] b, int off, int len){}

    Writer 字符输出流

    • public void write(int n){}
    • public void write(String str){}
    • public void write(char[] c){}

    传统读取文件的方式

    // 传统字节流读取
    psvm(String[] args){
      // 1. 创建FileInputStream对象
      FileInputSteam fis = new FileInputStream("路径");
      // 2. 读取
      int data = 0;
      while((data = fis.read()) != -1){
        sout((char)data); 
      }
      // 3. 关闭
      fis.close();
    }
    

      

    以上这种方式,如果文件中的内容是英文的,那是没有什么问题的,因为一个英文字母一个字节,但是当文件中的内容是中文的话,一个汉字是三个字节,这个时候,再读取文件中的内容是会出现乱码的。

     

    FileReader

    小案例;

     

    package com.iopractise;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class Demo10 {
        public static void main(String[] args) throws IOException {
            // 1. 创建FileReader 文件字符输入流
            FileReader fr = new FileReader("d:\aaa.txt");
            // 2. 读取
            // 2.1 单个字符读取
            int data = 0;
            while ((data = fr.read()) != -1) {
                System.out.println(((char) data));// 读取一个字符
            }
            //2.2 字符缓冲区读取
            char[] buf = new char[2];
            int count = 0;
            while ((count = fr.read(buf)) != -1) {
                System.out.println(new String(buf, 0, count));
            }
            // 3. 关闭
            fr.close();
        }
    }
    

     

      

    运行结果;

    或者

    中国

    加油

    当然如果文件中,既有中文又有英文也是没有问题的,都能够进行正常读取。

    h

    e

    l

    l

    o

    w

    o

    r

    l

    D

    FileWriter

    小案例:

    package com.iopractise;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Demo11 {
        public static void main(String[] args) throws IOException {
            // 1. 创建FileWriter对象
            FileWriter fw = new FileWriter("d:\ccc.txt");
            // 2. 写入
            for (int i = 0; i < 10; i++) {
                fw.write("中国加油!");
                fw.flush();
            }
            // 3. 关闭
            fw.close();
            System.out.println("执行完毕");
        }
    }
    

      

    运行结果:

    我们打开文件会看到我们写入的内容。

    中国加油!中国加油!中国加油!中国加油!中国加油!中国加油!中国加油!中国加油!中国加油!中国加油!

    使用FileReaderFileWriter来实现文件的复制操作

    package com.iopractise;
    
    import java.io.FileReader;
    import java.io.FileWriter;
    
    /**
     * 不能复制图片或二进制文件,使用字节流可以复制任意文件
     * 复制图片、音频等非字符文件只能使用字节流,因为在电脑中存储的一切文件的底层都是一个个字节。
     */
    public class Demo12 {
        public static void main(String[] args) throws Exception{
            // 1. 创建
            FileReader fr = new FileReader("d:\ccc.txt");
            FileWriter fw = new FileWriter("d:\ccc2.txt");
            // 2. 读写
            int data = 0;
            while((data = fr.read()) != -1){
                fw.write(data);
                fw.flush();
            }
            // 3. 关闭
            fw.close();
            fr.close();
        }
    }
    

      

     

     

  • 相关阅读:
    Python【每日一问】38
    Python【每日一问】37
    Shell~echo -e 颜色输出
    Python【每日一问】36
    Python【每日一问】35
    聊聊、Java 命令 第二篇
    聊聊、RabbitMQ 配置文件
    聊聊、Java 命令 第一篇
    聊聊、CA机构认证CSR生成
    聊聊、Tomcat中文乱码和JVM设置
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/14386947.html
Copyright © 2011-2022 走看看