Java 数据输入输出流 内存操作流 打印流

21.01数据输入输出流的概述和使用

  A:数据输入输出流的概述
  	 数据输入流:DataInputStream
  	 数据输出流:DataOutputStream
  	 特点:可以写基本数据类型,可以读取基本数据类型
public static void main(String[] args) throws IOException {
   
        //数据输入输出流,此流最大的特点,就是能够读写基本数据类型
        /*  数据输入流:
        DataInputStream
        数据输出流:
        DataOutputStream*/

        //writeData();
        //你怎么写的,你就怎么读取,顺序不要乱
        DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));
        int num = in.readInt();
        System.out.println(num);
        boolean b = in.readBoolean();
        System.out.println(b);
        String s = in.readUTF();
        System.out.println(s);
        double v = in.readDouble();
        System.out.println(v);
    }

    private static void writeData() throws IOException {
   
        DataOutputStream ds = new DataOutputStream(new FileOutputStream("a.txt"));
        ds.writeInt(500);
        ds.writeBoolean(true);
        ds.writeUTF("hahaha");  //字符串
        ds.writeDouble(3.14);
        ds.close();
    }

21.02_内存操作流的概述和使用

  A:内存操作流的概述
  	 a:操作字节数组
  	 	ByteArrayOutputStream
  	 	ByteArrayInputStream
  	 b:操作字符数组
  	 	CharArrayWrite
  	 	CharArrayReader
  	 c:操作字符串
  	 	StringWriter
  	 	StringReader
public static void main(String[] args) throws IOException {
   
        // 内存操作流: 不会关联文件,只是在内存中进行数据的读写,内存操作
        //流,会自己在内存中维护了一个缓存,把数据维护在缓存中
     /*   a:
        操作字节数组
        ByteArrayOutputStream
        ByteArrayInputStream
        此流关闭无效,所以无需关闭
        b:
        操作字符数组
        CharArrayWrite
        CharArrayReader
        c:
        操作字符串
        StringWriter
        StringReader*/

        // ByteArrayOutputStream
        //ByteArrayInputStream


      /*  此类实现了一个输出流,其中的数据被写入一个 byte 数组。
        缓冲区会随着数据的不断写入而自动增长。
        可使用 toByteArray () 和 toString () 获取数据。
        关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被
        调用,而不会产生任何 IOException。
      */
     /*   构造方法摘要
        ByteArrayOutputStream()
        创建一个新的 byte 数组输出流。
     */

        //自己在内存中维护了一个字节数组充当缓存,你用他写入的数据,就会放
        //到他维护的这个字节数组中, 缓冲区会随着数据的不断写入而自动增长
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write("aaa".getBytes());
        bos.write("bbb".getBytes());
        bos.write("ccc".getBytes());
        //取出ByteArrayOutputStream 他所维护的那个字节数组
        byte[] bytes = bos.toByteArray();
        String s = new String(bytes);
        System.out.println(s);
        //如果放的是,字符串的字节数据,你可以直接调用toString()
        String s2 = bos.toString();
        System.out.println(s2);
    }
public static void main(String[] args) throws IOException {
   
        /*  操作字节数组
                ByteArrayOutputStream
                ByteArrayInputStream*/
       /* ByteArrayInputStream( byte[] buf)
        创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。*/
        byte[] bytes = "西部开源科技教育有限公司".getBytes();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        byte[] bytes1 = new byte[1024];
        int len = in.read(bytes1);
        String s = new String(bytes1, 0, len);
        System.out.println(s);
    }
public static void main(String[] args) throws IOException {
   
        //把多个文件合并成一个文件
        //比如:我要把两首歌 合并成一首歌
        FileInputStream in1 = new FileInputStream("C:\\Users\\57642\\Desktop\\The Weeknd,Daft Punk - Starboy.mp3");
        FileInputStream in2 = new FileInputStream("C:\\Users\\57642\\Desktop\\Kanye West,Daft Punk - Stronger.mp3");
        FileOutputStream allOut = new FileOutputStream("C:\\Users\\57642\\Desktop\\The Weeknd.mp3");
        ArrayList<FileInputStream> list = new ArrayList<>();
        list.add(in1);
        list.add(in2);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte[] bytes = new byte[1024 * 8];
        int len = 0;
        for (FileInputStream in : list) {
   
            while((len = in.read(bytes)) != -1){
   
                bos.write(bytes,0,len);
            }
            in.close();
        }

        //取出两首歌的字节数据
        byte[] allBytes = bos.toByteArray();

        ByteArrayInputStream bin = new ByteArrayInputStream(allBytes);

        byte[] bytes2 = new byte[1024 * 8];
        int len2 = 0;

        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值