IO流的补充

一、直接操作内存的流

1、字符串流:

(1)StringReader(字符串读取流):从内存中来读取字符串

/**
 * 字符串流---字符串读取
 */
public class practise16 {
    public static void main(String[] args) {
        StringReader str = null;
        try {
            //构建字符串读取流
            str = new StringReader("hello");
            //定义变量记录每轮循环读到的字节
            int len;
            //循环读取
            while ((len = (str.read())) != -1){
                //输出
                System.out.println((char)len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
            //判空
            assert str != null;
            //关流
            str.close();
            }catch (NullPointerException e){
                e.printStackTrace();
            }finally {
                //手动关流
                str = null;
            }
        }
    }
}

(2)StringWriter(字符串输出流):将数据以字符串的形式写入到内存中

/**
 * 字符串流----字符串输出流
 */
public class practise17 {
    public static void main(String[] args) {
        StringWriter str = new StringWriter();
        str.write("hello");
        try {
            str.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、字节数组流:

(1)ByteArrayInputStream(字节数组输入流):从内存中来读取字节数组

/**
 * 字节数组输入流
 */
public class practise18 {
    public static void main(String[]args) {
        //构建字节数组
        byte[] bytes = {1,2,3,4,5,6};
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        int read = inputStream.read();
        System.out.println((char) read);
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

(2)ByteArrayOutputStream(字节数组输出流):将字节数组写入内存

/**
 * 字节数组输出流
 */
public class practise19 {
    public static void main(String[] args) {
        ByteArrayOutputStream outputStream = null;
        try {
            outputStream = new ByteArrayOutputStream();
            outputStream.write("冯宁柯".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                assert outputStream != null;
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                outputStream = null;
            }
        }
    }
}

二、打印流

将数据打印到指定的位置上,打印流只有输出流没有输入流,提供了便捷的打印方式,可以打印各种类型的数据。

1、PrintWriter:

/**
 * 打印流---printWriter
 */
public class practise20 {
    public static void main(String[] args) {
        PrintWriter printWriter = new PrintWriter(System.out);
        printWriter.print("冯宁柯");
        printWriter.print("冯宁柯");
        printWriter.print("冯宁柯");
        printWriter.close();
    }
}

2、PrintStream:

/**
 * 打印流---PrintStream
 */
public class practise21 {
    public static void main(String[] args) {
        PrintStream printStream = new PrintStream(System.out);
        printStream.print("冯宁柯");
        printStream.print("冯宁柯");
        printStream.print("冯宁柯");
        printStream.close();
    }
}

三、合并流(sequenceInputStream)

将多个流合并成一个流来进行读取,合并流只有输入流没有输出流。

/**
 * 合并流
 */
public class practise14 {
    public static void main(String[] args) throws IOException {
        //要被合并且指向不同文件的流
        FileInputStream in1 = new FileInputStream("D:\\a.txt");
        FileInputStream in2 = new FileInputStream("D:\\b.txt");
        FileInputStream in3 = new FileInputStream("D:\\c.txt");
        //创建向量---存放要合并的流
        Vector<InputStream> vector = new Vector<>();
        vector.add(in1);
        vector.add(in2);
        vector.add(in3);

        //获取迭代器---合并流需要
        Enumeration<InputStream> elements = vector.elements();
        //创建合并流
        SequenceInputStream sequenceInputStream = new SequenceInputStream(elements);
        //创建输出流----用于将合并后的流写出到文件中
        FileOutputStream out = new FileOutputStream("D:\\1.txt");

        //循环读写
        //块读
        byte[] bytes = new byte[10];
        int len;
        while ((len = (sequenceInputStream.read(bytes))) != -1){
            out.write(bytes, 0, len);
        }
        //关流
        out.close();
        in1.close();
        in2.close();
        in3.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊渊啊渊.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值