一、管道流

  final PipedOutputStream pps = new PipedOutputStream();
  final PipedInputStream pis = new PipedInputStream(pps);
  new Thread(new Runnable() {
   public void run() {
    try {
     pps.write("厉害了".getBytes(StandardCharsets.UTF_8));
     pps.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }).start();
  
  new Thread(new Runnable() {
   @Override
   public void run() {
    try {
     int b3 = 0;
     byte[] flush = new byte[1024];
     while ((b3=pis.read(flush)) != -1) {
      System.out.println(new String(flush, 0, flush.length));
     }
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }).start();
  • 上面的管道流和Linux中的管道流是类似的

二、基本数据流

  • 可以使用DataInputStream等及逆行基本数据的存储
  String address = "E:\\d05_gitcode\\Java\\newJava\\src\\com\\newJava\\newFile.txt";
  DataInputStream dis = new DataInputStream(new FileInputStream(address));
  byte b = dis.readByte();
  System.out.println(b);
  short s = dis.readShort();
  System.out.println(s);
  int i = dis.readInt();
  System.out.println(i);
  long l = dis.readLong();
  System.out.println(l);
  float f = dis.readFloat();
  System.out.println(f);
  double d = dis.readDouble();
  System.out.println(d);
  char c = dis.readChar();
  System.out.println(c);
  boolean bo = dis.readBoolean();
  System.out.println(bo);
  
  DataOutputStream dos = new DataOutputStream(new FileOutputStream(address));
  dos.writeByte(1);
  dos.writeShort(2);
  dos.writeInt(3);
  dos.writeLong(4L);
  dos.writeFloat(5.0f);
  dos.writeDouble(6.0d);
  dos.writeChar(7);
  dos.writeBoolean(false);
156.2
156.2

三、缓存流

  • CPU运行速度是内存的几百倍,是磁盘的几百万倍,因此减少和磁盘的交互,就能提高IO的速度,我们在这两者之间提供一个buffer,缓存流(也就是BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter), 来增多一次性读写的数量,减少IO的次数。 156.1

四、打印流

  • 我们所使用的System.out.println()方法就是打印流的一种
  StringWriter sw = new StringWriter();
  try (PrintWriter pw = new PrintWriter(sw)) {
   pw.println("lisudfos");
  }
  System.out.println("lisudfos");

五、源码

posted on 2021-12-27 00:13  心悦君兮君不知-睿  阅读(95)  评论(0编辑  收藏  举报