java 从入门到精通

java 中的IO(字符流)

2021-05-28  本文已影响0人  Exception_Cui

以字符的方式读取

java中的 中文 在不同的编码中 占据不同的字节,java中提供了 字符流的方式读取中文


image.png
package IOTest;

import java.io.FileReader;
import java.io.IOException;

/**
 * Created by kumamon on 2021/5/27.
 * 字符输入流(内存-硬盘)
 * 因为中文在不同的编码中 占得字节数不一样
 * 所以jdk 提供了读取字符的方法
 */
class FileReaderDemo {

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

        //fileReader1();
        fileReader2();
    }

    /**
     * 读取单个字节
     * */
    private static void fileReader1() throws IOException {
        FileReader fileReader=new FileReader("1.txt");
        int len=fileReader.read();
        System.out.println((char)len); //你
    fileReader.close();
    }

    /**
     * 读取多个字节
     * */
    private static void fileReader2() throws IOException {
        FileReader fileReader=new FileReader("1.txt");
        int len=0;
        char[] chars = new char[1024];
        if ((len=fileReader.read(chars))!=-1) {
            System.out.println(new String(chars));  //你154841545641你好 我是你爸爸
        }
        fileReader.close();
    }
}

字符缓冲流(输入)

    /**
     * 多了一个 readline() 一次性读取一行数据
     * read()方法和之前的一模一样
     * */
    private static void bufferReader() throws IOException {

        FileReader fileReader=new FileReader("bufferWirter.txt");
        BufferedReader bf=new BufferedReader(fileReader);
        /*int len=0;
        char[] chars = new char[1024];
        if ((len=fileReader.read(chars))!=-1) {
            System.out.println(new String(chars));  //你154841545641你好 我是你爸爸
        }
        fileReader.close();*/

        String line=null;
        while ((line = bf.readLine()) != null) {
            System.out.println(line);
        }
        bf.close();
    }

字符转换流(设置编码格式)

   /**
     * 字符转换流
     * */
    private static void utf8_or_gbk_reader() throws IOException {
        InputStreamReader isr=new InputStreamReader(new FileInputStream("utf-8.txt"),"gbk");
        int len=0;
        char[] chars = new char[1024];
        while ((len=isr.read(chars))!=-1){
            System.out.println(new String(chars));

        }
    }






以字符的方式写入

package IOTest;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by kumamon on 2021/5/27.
 * 内存---->硬盘
 * 
 * 注意事项,
 *
 * 因为电脑存储的是字节文件
 * 但是写入的方式是字符的方式(中间会有自动装箱 转换)
 * 
 *
 */
public class FileWirteDemo {
    public static void main(String[] args) throws IOException {
        fileWirte1();
    }

    private static void fileWirte1() throws IOException {
        FileWriter fw=new FileWriter("FileWirter.txt");
        fw.write("直接写String");  //直接写string
        fw.write('\n');             //换行
        fw.write(new char[]{'a','b','c'}); //写char数组
        
        /**
         * 使用字符流必须 将 内存中的刷新到硬盘中
         * 如果不刷新  那么硬盘中文件为 空
         * */
        //fw.flush();  //将字符流缓冲区(内存)的字符 刷新到 硬盘中
        //close 中默认调用了flush 所以可以省略
        
        fw.close();
    }
}

image.png

字符缓冲流(输出)

  /**
    *字符缓冲流
    * newLine();字符缓冲流特有的方法
    *
    */
    private static void bufferWirte() throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("bufferWirter.txt"));

        bw.write("我是字符缓冲流,使用newLine方法换行");  //直接写string
        bw.newLine();             //换行
        bw.write(new char[]{'a','b','c'}); //写char数组

        /**
         * 使用字符流必须 将 内存中的刷新到硬盘中
         * 如果不刷新  那么硬盘中文件为 空
         * */
        //fw.flush();  //将字符流缓冲区(内存)的字符 刷新到 硬盘中
        //close 中默认调用了flush 所以可以省略

        bw.close();

    }

字符转换流(设置编码格式)

*IDE是UTF-8 ,windowns 是GBK
*所以存储的解码就出现问题
* 就有了字符转换流


    /**
     *字符转换流
     * 乱码的问题一般都是指定的编码方式不一样
     * 例如
     * IDE是UTF-8   ,windowns 是GBK
     * 所以存储的解码就出现问题
     * 就有了字符转换流
     *
     *
     *
     *
     */
    private static void utf8_or_gbk_write() throws IOException {                                               // 此处指定编码 GBK
        OutputStreamWriter outputStreamWriter =new OutputStreamWriter(new FileOutputStream("utf-8.txt"),"utf-8");
        outputStreamWriter.write("我是设置了编码格式的字符流");
        //outputStreamWriter.flush(); //可以省略
        outputStreamWriter.close();
    }

上一篇 下一篇

猜你喜欢

热点阅读