IO(Input Output)流__字节流

时间:2023-03-09 04:00:03
IO(Input Output)流__字节流

续:

 ------->>>>字节流

   IntputStream   OutputStream

  需求:想要操作图片数据,就需要用到字节流。

  读写操作:

    FileOutputStream  FileInputStream

    <---写--->

    FileOutputStream fos = new FileOutputStream("fos.txt");

    fos.write("abcde".getBytes());

    fos.close();

  <---读--->

    FileInputStream fis = new FileInputStream("fos.txt");

    byte[] buf = new byte[fis.availabe()];  //定义一个刚刚好的缓冲区,不用再循环了。

    fis.read(buf);

    System.out.println(new String(buf));

    fis.colse();

  拷贝图片:

    思路:1、用字节读取流对象和图片关联;

       2、用字节写入流对象创建一个图片文件,用于存储获取到的图片数据;

         3、通过循环读写,完成数据的存储

       4、关闭资源

   部分代码如下:

     FileOutputStream fos = null;

     FileInputStream fis = null;

     try {

       fos = new FileOutputStream("Demo.bmp");

       fis = new FileInputStream("DemoCopy.bmp");

       byte[] buf = new byte[1024];

       int len = 0;

       while((len = fis.read(buf)) != -1) {

         fos.write(buf, 0, len);

       }

     }catch() {

       throw new RuntimeException("复制文件失败");

     }

     finally {

       try{

         if(fis != null)

           fis.close();

       }catch(IOException e) {

         throw new RuntimeException("读取关闭失败");

       }

       try{

         if(fos != null)

           fos.close();

       }catch(IOException e) {

         throw new RuntimeException("写入关闭失败");

       }

     }

   字节流的缓存区:

      演示mp3的复制,通过缓冲区。

      BufferedOutputStream

      BufferedInputStream

    public static void copy() throws IOException {

      BUfferedInputStream bufis = new BufferedInputStream(new FileInputStream("Demo.mp3"));

      BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(DemoCopy.mp3));

      int by = 0;

      while((by = bufis.read()) != -1) {

        bufos.write(by);

      }

      bufos.close();  bufis.close();

    }

  附加小练习:自定义一个缓冲区;

    

 读取键盘录入:

    System.out :  对应的标准输出设备, 控制台

    System.in :  对于的标准输入设备: 键盘

   需求:通过键盘录入数据,当录入一行数据后,就将该行数据进行打印; 如果录入的数据是over,那么停止录入。

     InputStream in = Sytem.in;
    StringBuilder sb = new StringBuilder();
    int ch = 0;     while(true) {       int ch = in.read();
      if(ch == '\r')         cntinue;
      if(ch == '\n') {         String s = sb.toString();
        if("over".equals(s))
          break;
        System.out.println(s.toUpperCase());
        sb.delete(0, sb.length());
      }
      else
        sb.append((char) ch);
      sb.append((char) ch) ;
    }

  读取转换流:

    //获取键盘录入对象。

      InputStream in = System.in;

    //将字节流对象转成字符流对象,使用转换流。 InputStreamReader

      InputStreamReader isr = new InputStreamReader(in);

    //为了提高效率,将字符串进行缓冲区技术高效操作,使用BufferedReader

      BufferedReader bufr = new BufferedReader(isr);

    加强:BufferReader bufr = 

          new BufferReader(new InputStreamReader(System.in));

      String line = null;

      while((line = bufr.readLine()) != null) {

        System.out.println(line.toUpperCase());

      }

      bufr.close();

  写入转换流:

     BufferedWriter bufw =

         new BufferedWriter(new OutputStreamWriter(System.out));

     

     String line = null;

     While((line = bufr.readLine()) != null) {

      if("over".equals(line)) {

       break;

      bufw.writer(line.toUpperCase());       

      bufw.newLine();

      bufw.flush();

     }

     bufr.close();