JavaSE——IO流7:其他流(内存流、系统流、合并流、随机流等)

目录

一、字符内存输入输出流—StringReader与StringWriter

二、字符数组输入输出流—CharArrayReader与CharArrayWriter

三、字节数组输入输出流—ByteArrayInputStream与ByteArrayOutputStream 

四、系统输入输出流—System.in与System.out

五、合并流——SequenceInputStream

六、随机获取流——RandomAccessFile(断点续传)


一、字符内存输入输出流—StringReader与StringWriter

private static void demo2() throws IOException {
    // 内存输出流
    StringWriter stringWriter = new StringWriter();
    // 向内存中写入字符串
    stringWriter.write("李白");
    stringWriter.close();
}

private static void demo1() throws IOException {
    // 内存输入流
    String str = "abcdef";
    StringReader stringReader = new StringReader(str);
    // 使用内存流读取字符串
    int len;
    char[] chars = new char[3];
    while ((len = stringReader.read(chars)) != -1) {
        System.out.print(new String(chars, 0, len));
    }
    stringReader.close();
    // 运行结果:abcdef
}

二、字符数组输入输出流—CharArrayReader与CharArrayWriter

private static void demo3() throws IOException {
    // 读取内存中的字符数组
    CharArrayReader charArrayReader = new CharArrayReader("李四".toCharArray());
    // 将字符数组写入内存
    CharArrayWriter charArrayWriter = new CharArrayWriter();
    int len;
    char[] chars = new char[3];
    while ((len = charArrayReader.read(chars)) != -1) {
        charArrayWriter.write(chars, 0, len);
    }
    charArrayWriter.close();
    charArrayReader.close();
}

三、字节数组输入输出流—ByteArrayInputStream与ByteArrayOutputStream 

private static void demo4() throws IOException {
    // 读取内存中的字节数组
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("张三".getBytes());
    // 将字节数组写入内存
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int len;
    byte[] bytes = new byte[2];
    while ((len = byteArrayInputStream.read(bytes)) != -1) {
        byteArrayOutputStream.write(bytes, 0, len);
    }
    byteArrayOutputStream.close();
    byteArrayInputStream.close();
}

四、系统输入输出流—System.in与System.out

System.out标准输出流PrintStream
System.in标准输入流InputStream
System.err标准错误流,输出的信息全部是红色,有高并发现象PrintStream

Scanner使用时不要关流scanner.colse();

System.in是一个静态常量,被多个Scanner共享,一个关了,其他也受影响

使用IO流从控制台获取一行数据

public class PrintDemo {
    public static void main(String[] args) throws IOException { 
        // 思路:从控制台获取数据,使用System.in
        //      IO流中BufferedReader有readLine方法,可以读取整行数据
        //      BufferedReader创建对象时,需要字符流
        //      用户输入的是字节,所以还要嵌套一个转换流,将字节流转换为字符流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String readLine = bufferedReader.readLine();
        System.out.println(readLine);
        bufferedReader.close();
    }
}

运行结果:

五、合并流——SequenceInputStream

合并流就是把多个流合并成一个流。

要求:格式一致,编码也要一致。

构造方法一: 

private static void demo1() throws IOException {
    FileInputStream s1 = new FileInputStream("D:\\ccc.txt");
    FileInputStream s2 = new FileInputStream("D:\\ddd.txt");
    SequenceInputStream sequenceInputStream = new SequenceInputStream(s1, s2);
    FileOutputStream fileOutputStream = new FileOutputStream("D:\\sequence2.txt", true);
    int len;
    byte[] bytes = new byte[6];
    while ((len = sequenceInputStream.read(bytes)) != -1) {
        fileOutputStream.write(bytes, 0, len);
    }
    fileOutputStream.close();
    sequenceInputStream.close();
}

D:\\sequence2.txt文件: 

构造方法二: 

private static void demo2() throws IOException {
    Vector<InputStream> vector = new Vector<>();
    vector.add(new FileInputStream("D:\\aaa.txt"));
    vector.add(new FileInputStream("D:\\bbb.txt"));
    vector.add(new FileInputStream("D:\\ccc.txt"));
    vector.add(new FileInputStream("D:\\ddd.txt"));
    // 创建合并流
    SequenceInputStream sequenceInputStream = new SequenceInputStream(vector.elements());
    // 创建字节输出流
    FileOutputStream fileOutputStream = new FileOutputStream("D:\\sequence.txt");
    // 读取到的实际个数
    int len;
    // 创建缓冲流数组
    byte[] bytes = new byte[6];
    while ((len = sequenceInputStream.read(bytes)) != -1) {
        fileOutputStream.write(bytes, 0, len);
    }
    // 关闭流
    fileOutputStream.close();
    sequenceInputStream.close();
}

D:\\sequence.txt文件:

六、随机获取流——RandomAccessFile(断点续传)

        随机获取流RandomAccessFile把文件看作一个字节数组,其中有一个索引在指定读取到的位置。

        随机获取流是一个双向流,可以读也可以写。

mode:模式

  • r 读

  • rw 读写

  • rws 读写并写入到硬盘

  • rwd 读写并立即写入到硬盘

        此类的实例支持读取和写入随机访问文件。 随机访问文件的行为类似于存储在文件系统中的大量字节。 有一种游标或索引到隐含数组中,称为文件指针 ;输入操作从文件指针开始读取字节,并使文件指针超过读取的字节数。

        如果在读/写模式下创建随机访问文件,则输出操作也可用; 输出操作从文件指针开始写入字节,并使文件指针超过写入的字节。 写入隐含数组当前末尾的输出操作会导致数组被扩展。 文件指针可以通过读取getFilePointer方法和由设置seek方法。
        通常,对于此类中的所有读取例程,如果在读取了所需的字节数之前达到文件结尾,则抛出EOFException (这是一种IOException )。

         如果由于文件结尾之外的任何原因无法读取任何字节,则抛出IOException以外的EOFException 。 特别是,如果流已经关闭, IOException可能抛出IOException 。

文件原始内容:

public class RandomAccessFileDemo {
    public static void main(String[] args) throws IOException {
        // 创建随机获取流
        RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\aaa.txt", "rw");
        // 写入123
        randomAccessFile.write("123".getBytes());
        // 读取数据
        System.out.println((char) randomAccessFile.read()); // d

        // 将游标设置为指定的值
        randomAccessFile.seek(0);
        System.out.println((char) randomAccessFile.read()); // 1

        // 跳过字节
        randomAccessFile.skipBytes(3);
        System.out.println((char) randomAccessFile.read()); // e

        // 关流
        randomAccessFile.close();
    }
}

写入后结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值