Java中的字符型文件流(FileReader,FileWriter)

字符型文件流

与字节型文件流不同,字节型文件流读取和写入的都是一个又一个的字节。

而字符型文件流操作的单位是一个又一个的字符,字符型流认为一个字母是一个字符,而一个汉字也是一个字符。

字符型文件流一般只能够用来操作一些文本格式的文件,即可以用记事本正常打开的文件。
(如:.txt .java .c .properties .html .js .xml)

字符型文件流解决了使用字节型文件流读写纯文本文件时可能发生的中文乱码问题,所以读写纯文本文件是字符型文件流的强项。

它的用法与字节型文件流(FileInputStream,FileOutputStream)基本一致,只不过它每次读写的数组类型是char[]而不是byte[]。

我们说所有带Reader字眼的都是字符型流。

而同时带Stream和Reader字眼的它们是字节字符转换流。

FileReader

继承关系:
在这里插入图片描述
常用构造方法:

  • FileReader(File file)
  • FileReader(String fileName)

常用方法:
在这里插入图片描述
常用的4个

  • read(char[] chars)
  • read(char[] cbuf,int off,int len)
  • skip(long n)
  • close()

简单尝试:
准备一个.txt文件:
在这里插入图片描述
读取它:

    public static void main(String[] args) {
        File file = new File("F:\\test\\Test.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            char[] chars = new char[10];//每次读取5个字符
            int count = fr.read(chars);
            
            while (count!=-1){
                String s = new String(chars, 0, count);
                System.out.println(s);
                count = fr.read(chars);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fr!=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

执行结果:
在这里插入图片描述
换行是我打印的时候加上去的(每读取10个字符打印一次)。

FileWriter

继承关系:
在这里插入图片描述
常用构造方法:

  • FileWriter(File file)
  • FileWriter(File file, boolean append)
  • FileWriter(String fileName)
  • FileWriter(String fileName, boolean append)

常用方法:
在这里插入图片描述

常用的6个

  • write(char[] cbuf)
  • write(String str)
  • write(String str,int off,int len)
  • write(String cbuf,int off,int len)
  • close()
  • flush()

简单尝试:

    public static void main(String[] args) {
        File file = new File("F:\\test\\log.txt");
        //log.txt可以不存在,test文件夹必须存在

        FileWriter fw = null;
        try {
            fw = new FileWriter(file,true);//要追加写入数据
            String str = "文字是人类用表义符号记录表达信息,使之传之久远的方式和工具。现代文字大多是记录语言的工具。人类往往先有口头的语言后产生书面文字,很多方言和小语种,有语言但没有文字。文字的不同体现了国家和民族的书面表达的方式和思维不同。文字使人类进入有历史记录的文明社会。\n" +
                    "Text is a way and tool for human beings to record and express information by means of symbolic meaning.Modern writing is mostly a tool for recording language.Humans tend to have spoken languages before they have written languages, many dialects and smaller languages that have languages but no words.The different characters reflect the different ways and thinking of the written expression of the country and nation.Writing enabled mankind to enter a civilization with a historical record.";
            char[] chars = str.toCharArray();
            fw.write(chars);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fw!=null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

执行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43598138/article/details/105715689
今日推荐