Java字符流-reader与writer

发布于:2024-04-19 ⋅ 阅读:(24) ⋅ 点赞:(0)

目录

1、字符流

1.1 Reader

1.2 Writer

1.3 字符流拷贝文件

1.3.1 字符流拷贝文件实现一

1.3.2 字符流拷贝文件实现二

1.4 字符流的异常处理

1.5 字符流的缓冲区

1.6 装饰器模式


1、字符流

计算机并不区分二进制文件与文本文件。所有的文件都是以二进制形式来存储的,因此,从本质上说,所有的文件都是二进制文件。所以字符流是建立在字节流之上的,它能够提供字符层次的编码和解码。例如,在写入一个字符时,Java虚拟机会将字符转为文件指定的编码(默认是系统默认编码),在读取字符时,再将文件指定的编码转化为字符。

那么我们之前学习的流称之为字节流,以字节为单位进行操作之前的操作全是英文,如果想要操作中文呢?

测试:将指定位置的文件通过字节流读取到控制台

public class TestIo {

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

        String path = "c:\\a.txt";

        writFileTest();

        readFileByInputStream(path);

    }

    private static void readFileByInputStream(String path) throws IOException {

        FileInputStream fis = new FileInputStream(path);

        int len = 0;

        while ((len = fis.read()) != -1) {

            System.out.print((char) len);

        }

    }

    private static void writFileTest() throws FileNotFoundException,

            IOException {

        // 创建文件对象

        File file = new File("c:\\a.txt");

        // 创建文件输出流

        FileOutputStream fos = new FileOutputStream(file);

        fos.write("中国".getBytes());

        fos.close();

    }

}

发现控制台输出的信息:

???ú  是这样的东西,打开a.txt 文本发现汉字中国确实写入成功。

那么说明使用字节流处理中文有问题。

仔细分析,我们的FileInputStream输入流的read() 一次是读一个字节的,返回的是一个int显然进行了自动类型提升。那么我们来验证一下中国对应的字节是什么

使用:"中国".getBytes() 即可得到字符串对应的字节数组。是[-42, -48, -71, -6]

同样,将read方法返回值直接强转为byte ,发现结果也是-42, -48, -71, -6

代码:

public class TestIo {

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

        String path = "c:\\a.txt";

        writFileTest();

        readFileByInputStream(path);

        //查看中国对应的编码

        System.out.println(Arrays.toString("中国".getBytes()));

    }

    private static void readFileByInputStream(String path) throws IOException {

        FileInputStream fis = new FileInputStream(path);

        int len = 0;

        while ((len = fis.read()) != -1) {

            System.out.println((byte)len);

        }

    }

    private static void writFileTest() throws FileNotFoundException,

            IOException {

        // 创建文件对象

        File file = new File("c:\\a.txt");

        // 创建文件输出流

        FileOutputStream fos = new FileOutputStream(file);

        fos.write("中国\r\n".getBytes());

        fos.close();

    }

}

那么中国 对应的是-42, -48, -71, -64个字节。 那就是一个中文占2个字节,(这个和编码是有关系的)

很显然,我们的中文就不能够再一个字节一个字节的读了。所以字节流处理字符信息时并不方便那么就出现了字符流。

字节流是 字符流是以字符为单位。

体验字符流:

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

        String path = "c:\\a.txt";

        readFileByReader(path);

    }

private static void readFileByReader(String path) throws IOException {

        FileReader fr = new FileReader(path);

        int len = 0;

        while ((len = fr.read()) != -1) {

            System.out.print((char) len);

        }

    }

总结:字符流就是:字节流 + 编码表,为了更便于操作文字数据。字符流的抽象基类:

Reader , Writer。

由这些类派生出来的子类名称都是以其父类名作为子类名的后缀,如FileReader、FileWriter。

1.1 Reader

方法:

1,int read():

读取一个字符。返回的是读到的那个字符。如果读到流的末尾,返回-1.

2,int read(char[]):

将读到的字符存入指定的数组中,返回的是读到的字符个数,也就是往数组里装的元素的个数。如果读到流的末尾,返回-1.

3,close()

读取字符其实用的是window系统的功能,就希望使用完毕后,进行资源的释放

由于Reader也是抽象类,所以想要使用字符输入流需要使用Reader的实现类。查看API文档。找到了FileReader。

1,用于读取文本文件的流对象。

2,用于关联文本文件。

构造函数:在读取流对象初始化的时候,必须要指定一个被读取的文件。

如果该文件不存在会发生FileNotFoundException.

public class IoTest1_Reader {

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

        String path = "c:/a.txt";

        readFileByReader(path);

    }

   

private static void readFileByReader(String path) throws IOException {

        FileReader fr = new FileReader(path);

        int len = 0;

        while ((len = fr.read()) != -1) {

            System.out.print((char) len);

        }

    }

1.2 Writer

Writer中的常见的方法:

1,write(ch): 将一个字符写入到流中。

2,write(char[]): 将一个字符数组写入到流中。

3,write(String): 将一个字符串写入到流中。

4,flush():刷新流,将流中的数据刷新到目的地中,流还存在。

5,close():关闭资源:在关闭前会先调用flush(),刷新流中的数据去目的地。然流关闭。

发现基本方法和OutputStream 类似,有write方法,功能更多一些。可以接收字符串。

同样道理Writer是抽象类无法创建对象。查阅API文档,找到了Writer的子类FileWriter

1:将文本数据存储到一个文件中。

public class IoTest2_Writer {

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

        String path = "c:/ab.txt";

        writeToFile(path);

    }

    /**

     * 写指定数据到指定文件中

     */

    public static void writeToFile(String path) throws Exception {

        Writer writer = new FileWriter(path);

        writer.write('');

        writer.write("世界".toCharArray());

        writer.write("中国");

        writer.close();

    }

}

2:追加文件:

默认的FileWriter方法新值会覆盖旧值,想要实现追加功能需要

使用如下构造函数创建输出流 append值为true即可。

FileWriter(String fileName, boolean append)

FileWriter(File file, boolean append)

3:flush方法

   如果使用字符输出流,没有调用close方法,会发生什么?

private static void writeFileByWriter(File file) throws IOException {

        FileWriter fw = new FileWriter(file);

        fw.write('');

fw.flush();

        fw.write("中国".toCharArray());

        fw.write("世界你好!!!".toCharArray());

        fw.write("明天");

        // 关闭流资源

        //fw.close();

    }

程序执行完毕打开文件,发现没有内容写入.原来需要使用flush方法. 刷新该流的缓冲。

为什么只要指定close方法就不用再flush方法,因为close也调用了flush方法.

1.3 字符流拷贝文件

   一个文本文件中有中文有英文字母,有数字。想要把这个文件拷贝到别的目录中。

我们可以使用字节流进行拷贝,使用字符流呢?肯定也是可以的。

1.3.1 字符流拷贝文件实现一

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

        String path1 = "c:/a.txt";

        String path2 = "c:/b.txt";

        copyFile(path1, path2);

    }

/**

     * 使用字符流拷贝文件

     */

    public static void copyFile(String path1, String path2) throws Exception {

        Reader reader = new FileReader(path1);

        Writer writer = new FileWriter(path2);

        int ch = -1;

        while ((ch = reader.read()) != -1) {

            writer.write(ch);

        }

        reader.close();

        writer.close();

    }

但是这个一次读一个字符就写一个字符,效率不高。把读到的字符放到字符数组中,再一次性的写出。

1.3.2 字符流拷贝文件实现二

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

        String path1 = "c:/a.txt";

        String path2 = "c:/b.txt";

        copyFile(path1, path2);

    }

public static void copyFile3(String path1, String path2) throws Exception{

        Reader reader = new FileReader(path1);

        Writer writer = new FileWriter(path2);

        int ch = -1;

        char [] arr=new char[1024];

        while ((ch = reader.read(arr)) != -1) {

            writer.write(arr,0,ch);

        }

        reader.close();

        writer.close();

    }

字节流可以拷贝视频和音频等文件,那么字符流可以拷贝这些吗?

经过验证拷贝图片是不行的。发现丢失了信息,为什么呢?

计算机中的所有信息都是以二进制形式进行的存储(1010)图片中的也都是二进制

在读取文件的时候字符流自动对这些二进制按照码表进行了编码处理,但是图片本来就是二进制文件,不需要进行编码。有一些巧合在码表中有对应,就可以处理,并不是所有的二进制都可以找到对应的。信息就会丢失。所以字符流只能拷贝以字符为单位的文本文件

(以ASCII码为例是127个,并不是所有的二进制都可以找到对应的ASCII,有些对不上的,就会丢失信息。)

1.4 字符流的异常处理

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

        String path1 = "c:/a.txt";

        String path2 = "c:/b.txt";

        copyFile2(path1, path2);

    }

/**

     * 使用字符流拷贝文件,有完善的异常处理

     */

    public static void copyFile2(String path1, String path2) {

        Reader reader = null;

        Writer writer = null;

        try {

            // 打开流

            reader = new FileReader(path1);

            writer = new FileWriter(path2);

            // 进行拷贝

            int ch = -1;

            while ((ch = reader.read()) != -1) {

                writer.write(ch);

            }

        } catch (Exception e) {

            throw new RuntimeException(e);

        } finally {

            // 关闭流,注意一定要能执行到close()方法,所以都要放到finally代码块中

            try {

                if (reader != null) {

                    reader.close();

                }

            } catch (Exception e) {

                throw new RuntimeException(e);

            } finally {

                try {

                    if (writer != null) {

                        writer.close();

                    }

                } catch (Exception e) {

                    throw new RuntimeException(e);

                }

            }

        }

    }

1.5 字符流的缓冲区

查看Reader 发现Reader,操作的是字符,我们就不需要进行编码解码操作,由字符流读到二进制,自动进行解码得到字符,写入字符自动编码成二进制.

Reader有一个子类BufferedReader。子类继承父类显然子类可以重写父类的方法,也可以增加自己的新方法。例如一次读一行就是常用的操作.那么BufferedReader 类就提供了这个方法,可以查看readLine()方法具备 一次读取一个文本行的功能。很显然,该子类可以对功能进行增强。

体验BufferedReader

public class IoTest_BufferedReader {

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

        readFile("c:\\a.txt");

    }

    private static void readFile(String path) throws IOException {

        Reader read = new FileReader(path);

        BufferedReader br = new BufferedReader(read);

        String line = null;

        while ((line = br.readLine()) != null) {

            System.out.println(line);

        }

    }

}

注意:

在使用缓冲区对象时,要明确,缓冲的存在是为了增强流的功能而存在,所以在建立缓冲区对象时,要先有流对象存在.

缓冲区的出现提高了对流的操作效率。原理:其实就是将数组进行封装。

使用字符流缓冲区拷贝文本文件.

public class Demo7 {

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

        // 关联源文件

        File srcFile = new File("c:\\linux大纲.txt");

        // 关联目标文件

        File destFile = new File("d:\\linux大纲.txt");

        // 实现拷贝

        copyFile(srcFile, destFile);

    }

    private static void copyFile(File srcFile, File destFile)

            throws IOException {

        // 创建字符输入流

        FileReader fr = new FileReader(srcFile);

        // 创建字符输出流

        FileWriter fw = new FileWriter(destFile);

        // 字符输入流的缓冲流

        BufferedReader br = new BufferedReader(fr);

        // 字符输出流的缓冲流

        BufferedWriter bw = new BufferedWriter(fw);

        String line = null;

        // 一次读取一行

        while ((line = br.readLine()) != null) {

            // 一次写出一行.

            bw.write(line);

            // 刷新缓冲

            bw.flush();

            // 进行换行,由于readLine方法默认没有换行.需要手动换行

            bw.newLine();

        }

        // 关闭流

        br.close();

        bw.close();

    }

}

1.6 装饰器模式

需求:想要在读取的文件的每一行添加行号。

public class IoTest7_BufferedReader {

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

        readFile("c:\\a.txt");

    }

    private static void readFile(String path) throws IOException {

        Reader read = new FileReader(path);

        BufferedReader br = new BufferedReader(read);

        int count = 0;

        String line = null;

        while ((line = br.readLine()) != null) {

            count++;

            System.out.println(count+":"+line);       

        }

    }

}

很容易的就可以实现。如果每次使用BufferedReader 输出时都需要显示行号呢? 每次都加? 很显然,我们的BufferedReader继承了Reader 对父类进行了功能的增强,那么我们也可以继承BufferedReader 重写该类的readLine方法,进行功能的增强.

public class IoTest_BufferedReader {

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

        readFile("c:\\a.txt");

    }

    private static void readFile(String path) throws IOException {

        Reader read = new FileReader(path);

        BufferedReader br = new MyBufferedReader(read);

        String line = null;

        while ((line = br.readLine()) != null) {

            System.out.println(line);}

    }

}

class MyBufferedReader extends BufferedReader {

    public MyBufferedReader(Reader read) {

        super(read);

    }

    int count;

    @Override

    public String readLine() throws IOException {

        String line = super.readLine();

        if (line != null) {

            count++;

            return count + ":" + line;

        } else {

            return null;

        }

    }

}

需求:

要在输出的一行前加上引号

可以再定义一个BufferedReader的子类,继承BufferedReader增强功能.

public class IoTest_BufferedReader {

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

        readFile("c:\\a.txt");

    }

    private static void readFile(String path) throws IOException {

        Reader read = new FileReader(path);

        BufferedReader br = new MyQutoBufferedReader(read);

        int count = 0;

        String line = null;

        while ((line = br.readLine()) != null) {

            System.out.println(line);

            count++;

        }

    }

}

// quotation 引号

class MyQutoBufferedReader extends BufferedReader {

    public MyQutoBufferedReader(Reader reader) {

        super(reader);

    }

    public String readLine() throws IOException {

        String line = super.readLine();

        if (line != null) {

            return "\"" + line + "\"";

        } else {

            return null;

        }

    }

}

需求三:

既想要显示行号又想要显示引号

发现,就需要再定义子类,发现这样比较麻烦,代码臃肿.而且代码重复.

可以换一种方式.如下:

其实就是一个新类要对原有类进行功能增强.

1. 在增强类中维护一个被增强的父类引用变量

       2. 在增强类的构造函数中初始化1中的变量

       3. 创建需要增强的方法,在刚方法中调用被被增强类的方法,并加以增强。

public class IoTest_BufferedReader {

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

        readFile("c:\\a.txt");

    }

    private static void readFile(String path) throws IOException {

        Reader read = new FileReader(path);

        BufferedReader bufferedReader = new BufferedReader(read);

        BufferedReader br = new MyQutoBufferedReader2(bufferedReader);

        br = new MyLineBufferedReader2(br);

        String line = null;

        while ((line = br.readLine()) != null) {

            System.out.println(line);

        }

    }

}

// quotation 引号

class MyQutoBufferedReader2 extends BufferedReader {

    private BufferedReader bufferedReader;

    public MyQutoBufferedReader2(BufferedReader bufferedReader) {

        super(bufferedReader);

        this.bufferedReader = bufferedReader;

    }

    public String readLine() throws IOException {

        String line = super.readLine();

        if (line != null) {

            return "\"" + line + "\"";

        } else {

            return null;

        }

    }

}

class MyLineBufferedReader2 extends BufferedReader {

    private BufferedReader bufferedReader;

    public MyLineBufferedReader2(BufferedReader bufferedReader) {

        super(bufferedReader);

        this.bufferedReader = bufferedReader;

    }

    int count;

    @Override

    public String readLine() throws IOException {

        String line = super.readLine();

        if (line != null) {

            count++;

            return count + ":" + line;

        } else {

            return null;

        }

    }

}

这就是装饰器模式

装饰器模式:

JavaIO中的应用:

   Java I/O类库需要多种不同的功能组合,所以使用了装饰器模式。

   FilterXxx类是JavaIO提供的装饰器基类,即我们要想实现一个新的装饰器,就要继承这些类。

装饰器与继承:

问题:

   修饰模式做的增强功能按照继承的特点也是可以实现的,为什么还要提出修饰设计模式呢?

继承实现的增强类和修饰模式实现的增强类有何区别?

   继承实现的增强类:

      优点:代码结构清晰,而且实现简单

     缺点:对于每一个的需要增强的类都要创建具体的子类来帮助其增强,这样会导致继承体系过于庞大。

修饰模式实现的增强类:

      优点:内部可以通过多态技术对多个需要增强的类进行增强

      缺点:需要内部通过多态技术维护需要增强的类的实例。进而使得代码稍微复杂。