Java的输入/输出——字符流

字符输入流:Reader类

字符输入流类都是Reader的子类,Reader类是一个抽象类,方法与InputStream类类似;
读取文件内容:

public static void main(String[] args) {
        File file = new File("D:\\lzq/java.txt");
        reader(file);

    }
    /**
     * 读取文件内容
     * @param file
     */
    public static void reader(File file) {
        Reader read = null;  //声明Reader对象
        char[] c = new char[(int)file.length()];   //根据文件大小定义数组
        try {
            read = new FileReader(file); //使用FileReader实例化Reader
            read.read(c);        //读取内容到数组
            read.close();
        }catch(IOException e) {
            System.out.println("读取失败");
            e.printStackTrace();
        }
        System.out.println(c);
    }

运行结果:

Java是一种编程语言Java是一种编程语言Java是一种编程语言

也可以使用循环的方法读取:

/**
     * 读取文件内容
     * @param file
     */
    public static void reader(File file) {
        Reader read = null;  //声明Reader对象
        char[] c = new char[(int)file.length()];   //根据文件大小定义数组
        int tmp = 0;
        int len = 0;
        try {
            read = new FileReader(file); //使用FileReader实例化Reader
            while((tmp = read.read()) != -1) {
                c[len] = (char)tmp;
                len++;
            }
            read.close();
        }catch(IOException e) {
            System.out.println("读取失败");
            e.printStackTrace();
        }
        System.out.println(c);
    }

字符输出流:Writer类

Writer类和OutputStream类,在功能上是一致的,但两者前者写入的是字符,后者写入的是字节,Writer本身也是一个抽象类:
这里写图片描述

1、向文件写入内容

    /**
     * 向文件写入内容
     * @param file
     */
    public static void writer(File file) {
        String str = "李正全";
        char[] c = str.toCharArray();  //将字符串转换为char数组
        try {
            Writer writer = new FileWriter(file);   //实例化对象
            writer.write(c);    //写入数据
            writer.close();
            System.out.println("写入成功");
        }catch(IOException e) {
            System.out.println("写入失败");
            e.printStackTrace();
        }
    }

2、向文件追加内容
使用字符输出流操作是=时,可实现文件的追加功能,有两种:

  1. 使用Writer的append()方法,实现内容的追加,但是无论重复多少次,写入大的内容都是相同的(意思就是你重复执行该程序,后面的会覆盖前面的,重复多次和运行一次效果一样)
  2. 使用FileWriter的构造方法FileWriter(File file,boolean append),可以不断更新内容(这种方法不覆盖)

字节流和字符流的区别:
字节流是对文件本身之间操作,不需要提供缓冲区;而字符流则需要提供缓冲区来操作文件,也就是说,使用字节流时,就算没有执行close()方法关闭字节流的操作,还是可以向文件输出内容的,但是在字符流中,若不执行close()方法,就无法向文件写入内容:

字节流操作时:

    /**
     * 给文件写入内容
     * @param file
     */
    public static void output(File file) {
        OutputStream output = null; //声明OutputStream对象
        String str = "Java是一种编程语言"; //创建字符串
        byte[] b = str.getBytes();     //将字符串转化成byte数组
        try {
            output = new FileOutputStream(file,true);  //使用FileOutputStream实例化OutputStream
            output.write(b);  //写入数据
            //output.close();    //关闭流,字节操作,不关闭流
            System.out.println("写入成功");
        }catch(IOException e) {
            System.out.println("写入失败");
            e.printStackTrace();
        }
    }

运行结果:

这里写图片描述

字符操作时:

/**
     * 向文件写入内容
     * @param file
     */
    public static void writer(File file) {
        String str = "李正全";
        char[] c = str.toCharArray();  //将字符串转换为char数组
        try {
            Writer writer = new FileWriter(file);   //实例化对象
            writer.write(c);    //写入数据
            //writer.close();
            System.out.println("写入成功");
        }catch(IOException e) {
            System.out.println("写入失败");
            e.printStackTrace();
        }
    }

这里写图片描述

从运行结果可以发现文件没有任何内容,因为程序在执行过程中引入了缓冲区机制,即写操作先将内容放在缓冲区,若写操作完毕后,不将缓冲区内容刷新到文件,文件将为空,但可以使用flush()方法强制清除内容:

/**
     * 向文件写入内容
     * @param file
     */
    public static void writer(File file) {
        String str = "李正全";
        char[] c = str.toCharArray();  //将字符串转换为char数组
        try {
            Writer writer = new FileWriter(file);   //实例化对象
            writer.write(c);    //写入数据
            writer.flush();    //强制清除
            //writer.close();
            System.out.println("写入成功");
        }catch(IOException e) {
            System.out.println("写入失败");
            e.printStackTrace();
        }
    }

这里写图片描述

猜你喜欢

转载自blog.csdn.net/QQ2899349953/article/details/81188343