• 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、关闭资源

       部分代码如下:

     1     FileOutputStream fos = null;
     2 
     3     FileInputStream fis = null;
     4 
     5     try {
     6 
     7       fos = new FileOutputStream("Demo.bmp");
     8 
     9       fis = new FileInputStream("DemoCopy.bmp");
    10 
    11       byte[] buf = new byte[1024];
    12 
    13       int len = 0;
    14 
    15       while((len = fis.read(buf)) != -1) {
    16 
    17         fos.write(buf, 0, len);
    18 
    19       }
    20 
    21     }catch() {
    22 
    23       throw new RuntimeException("复制文件失败");
    24 
    25     }
    26 
    27     finally {
    28 
    29       try{
    30 
    31         if(fis != null)
    32 
    33           fis.close();
    34 
    35       }catch(IOException e) {
    36 
    37         throw new RuntimeException("读取关闭失败");
    38 
    39       }
    40 
    41       try{
    42 
    43         if(fos != null)
    44 
    45           fos.close();
    46 
    47       }catch(IOException e) {
    48 
    49         throw new RuntimeException("写入关闭失败");
    50 
    51       }
    52 
    53     }

       字节流的缓存区:

          演示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,那么停止录入。

     1     InputStream in = Sytem.in;
     2     StringBuilder sb = new StringBuilder();
     3     int ch = 0;
     4 
     5     while(true) {
     6 
     7       int ch = in.read();
     8       if(ch == '
    ') 
     9 
    10         cntinue;
    11       if(ch == '
    ') {
    12 
    13         String s = sb.toString();
    14         if("over".equals(s)) 
    15           break;
    16         System.out.println(s.toUpperCase());
    17         sb.delete(0, sb.length());
    18       }
    19       else
    20         sb.append((char) ch);
    21       sb.append((char) ch) ;
    22     }

      读取转换流:

        //获取键盘录入对象。

          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();

     

  • 相关阅读:
    高并发下缓存失效问题及解决方案
    行为型设计模式
    Redisson
    行为型设计模式
    Docker 安装 Elasticsearch 和 Kibana
    行为型设计模式
    C# 使用 WebBrowser 实现 HTML 转图片功能
    .NET 程序下锐浪报表 (Grid++ Report) 的绿色发布指南
    .NET 程序员的 Playground :LINQPad
    Windows 服务器上的 WordPress 站点优化笔记
  • 原文地址:https://www.cnblogs.com/jesonjason/p/5117765.html
Copyright © 2020-2023  润新知