Java进阶学习之IO流(2)

1.流的概述

1.1.IO流是什么

我们生活中遇到的流有河流,河流的特点是自上而下的、从源点向终点运输水的通道。
那么IO流就是计算机中建立于数据源和目的地之间的运送数据的通道。

1.2.IO流的分类

按照数据类型分:

  • 字节流:传送的数据是字节
  • 字符流:传送的数据是字符

按照流向分:相对于程序来说

  • 输入流:别的地方输入
  • 输出流:输出到别的地方

1.3.字符编码

字符编码在程序中很重要,如果不处理好可能会导致乱码。
常见的字符编码:

  • ISO8859-1:单字节编码
  • GBK/GB2312:中文的国际编码
  • Unicode:一种编码规范,Java内部使用该编码
  • UTF:兼容ISO8859-1

2.字节流

2.1.InputStream输入字节流

抽象类,是所有输入字节流的父类,类结构如下:

该类中有以下方法:

常用方法介绍:

方法名 作用
int read() 从流中读取8个字节并且将8个字节转化成整数返回,即返回值为-1-255,如果返回-1即表示读取结束
int read(byte[]) 从流中读取指定大小的字节到数组中并返回读取的字节数,返回值为-1到Integer.MAX_VALUE
int read(byte[], int, int) 从流中读取指定大小的字节到数组中并将从指定位置读取指定长度字节转化成整数返回,返回值为-1到Integer.MAX_VALUE
long skip(long) 从流中跳过指定长度字节并返回实际跳过的字节数
void close() 关闭流
由于InputStream是抽象类,接下来我们用其实现类进行演示。
2.1.1.FileInputStream字节文件输入流

从本地文件中读取数据。
例子:
test文件内容:

hello world!
public class TestFileInputStream {
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream(new File("G:\\test"));
        byte[] bytes = new byte[1024];
        int len = is.read(bytes);
        is.close();
        System.out.println(len);
        System.out.println(new String(bytes, 0, len));
    }
}

执行结果:

2.1.2.ByteArrayInputStream字节数组输入流

从byte数组中读取数据。
例子:

public class TestByteArrayInputStream {
    public static void main(String[] args) throws IOException {
        String str = "Hello,World!";
        InputStream is = new ByteArrayInputStream(str.getBytes());
        byte[] bytes = new byte[1024];
        int len = is.read(bytes);
        is.close();
        System.out.println(len);
        System.out.println(new String(bytes, 0, len));
    }
}

2.2.OutputStream输出字节流

抽象类,是所有输出字节流的父类,类结构如下:

类中有以下方法:

常用方法介绍:

方法名 作用
void write(byte[]) 向输出流中写入字节数组中所有的字节
void write(byte[], int, int) 向输出流中写入指定大小的字节到数组中并返回读取的字节数
void write(int) 向输出流中写入一个字节
void flush() 将缓冲区的数据写入输出流并清除缓存
void close() 关闭流
2.2.1.FileOutputStream字节文件输出流

将数据写入文件中。
例子:

public class TestFileOutputStream {
    public static void main(String[] args) throws IOException {
        String str = "Hello,World!";
        OutputStream os = new FileOutputStream(new File("test.txt"));
        os.write(str.getBytes());
        os.flush();
        os.close();
    }
}

执行结果:

2.2.2.ByteArrayOutputStream字节数组输出流

将数据写入字节数组。
例子:

public class TestByteArrayOutputStream {
    public static void main(String[] args) throws IOException {
        String str = "Hello,World!";
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        os.write(str.getBytes(), 0, 5);
        os.flush();
        System.out.println(os.size());
        System.out.println(os.toString());
        os.close();
    }
}

执行结果:

3.字节流

3.1.Reader字符输入流

抽象类,是所有字符输入流的父类,类结构如下:

该类有以下几种方法:

常用方法介绍:

方法名 作用
int read() 从流中读取1个字符将其转化成整数返回,并返回值为-1-65535,如果返回-1即表示读取结束
int read(char[]) 从流中读取若干字符将其保存到字符数组中,并返回读取的字符数,如果返回-1即表示读取结束
int read(char[], int, int) 从流中读取指定起始和长度的若干字符将其保存到字符数组中,并返回读取的字符数,如果返回-1即表示读取结束
long skip() 从流中跳过指定长度字节并返回实际跳过的字符数
3.1.1.FileReader字符文件输入流

从文件中读取数据。
例如:

public class TestFileReader {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader(new File("test.txt"));
        char[] chars = new char[1024];
        int len = 0;
        while ((len = fr.read(chars)) != -1) {
            System.out.println(len);
            System.out.println(chars);
        }
        fr.close();
    }
}

执行结果:

3.1.2.BufferedRead字符缓冲区输入流

装饰流,用来将数据读取到缓存中,提高了数据的读取效率。
数据源:

例子:

public class TestBufferedReader {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("test.txt");
        BufferedReader br = new BufferedReader(fileReader);
        String str;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
    }
}

执行结果:

3.2.Writer字符输出流

抽象类,是所有字符输入流的父类,类结构如下:

类中有以下方法:

常用方法介绍:

方法名 作用
void write(int) 向流中写入一个字符
void write(char[]) 向流中写入字符数组的所有字符
void write(char[], int, int) 向流中写入指定起始位置到设定长度的字符
void write(String) 向流中写入一个字符串
void write(String, int, int) 向流中写入指定起始位置到设定长度的字符串
void append(char) 将字符添加到流中
void append(CharSequence) 将一个CharSequence添加到流中
void append(CharSequence, int, int) 将一个指定起始位置到终点位置的CharSequence添加到流中
void flush() 将缓冲区的数据写入输出流并清除缓存
void close() 关闭流
3.2.1.FilerWriter字符文件输出流

将数据写入文件中。
例子:

public class TestFileWriter {
    public static void main(String[] args) throws IOException {
        FileWriter fr = new FileWriter(new File("test.txt"));
        String str = "Hello,World!";
        fr.write(str, 0, 5);
        fr.flush();
        fr.close();
    }
}

执行结果:

3.2.2.BufferedWriter字符缓冲区输出流

装饰流,将数据写入缓存中,当缓存满了后一次性写入输出流中。
例子:

public class TestBufferedWriter {
    public static void main(String[] args) throws IOException {
        FileWriter fileReader = new FileWriter("test.txt");
        BufferedWriter bw = new BufferedWriter(fileReader);
        bw.write("Hello,World!");
        bw.newLine();
        bw.write("Hello,Java!");
        bw.flush();
        bw.close();
    }
}

执行结果:

posted @ 2020-12-21 11:17  liquorppp  阅读(79)  评论(0编辑  收藏  举报