package org.example.io;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**

* ByteArrayInputStream:在创建对象时传入数组即可,不需要传文件,也没有新增的方法,close()关闭无效

* 流本身就是内存中的资源,流中的内容也是内存中的资源,所以不用手动关闭,内存会给他释放,所以不用关闭流

* 流的内容是内存中的资源,不访问磁盘

*/

public class TestByteArrayInputStream {

  public static void main(String[] args) {

    String s = "Hello World!";

    byte[] b = s.getBytes();  /* 获取字符串的字节数组 */

    read(b);

  }

  private static void read(byte[] b) {

    ByteArrayInputStream bais = new ByteArrayInputStream(b);  /* ByteArrayInputStream实例化时不抛出异常的原因是没有和外界产生关系 */

    int i = 0;

    byte[] b1 = new byte[1024];  /* 字节数组缓冲区 */

    StringBuilder sb = new StringBuilder();  /* StringBuilder位于java.lang包中,类似于字符串 */

    try {

      while ((i = bais.read(b1)) != -1) {

        sb.append(new String(b1, 0, i));  /* 将指定的字符串循环附加到sb上 */

      }

      System.out.println(sb);  /* 输出sb字符串 */

    } catch (IOException e) {

      e.printStackTrace();

    }

  }

}

ByteArrayInputStream的更多相关文章

  1. Java 利用 ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件

    最近参与了github上的一个开源项目 Mycat,是一个mysql的分库分表的中间件.发现其中读取配置文件的代码,存在频繁多次重复打开,读取,关闭的问题,代码写的很初级,稍微看过一些框架源码的人,是 ...

  2. Java字节流:ByteArrayInputStream ByteArrayOutputStream

    ----------------------------------------------------------------------------------- ByteArrayInputSt ...

  3. ByteArrayInputStream和ByteArrayOutputStream

    public class ByteArrayTest { public static void main(String[] args) throws IOException { read(write( ...

  4. 【转】ByteArrayOutputStream和ByteArrayInputStream详解

    ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的 ...

  5. ByteArrayInputStream与ByteArrayOutputStrean的使用

    String str="sdfasdfasdfa加减法爱的色放就阿克苏地方啊"; InputStream is=new ByteArrayInputStream(str.toStr ...

  6. java基础知识回顾之javaIO类--内存操作流ByteArrayInputStream和ByteArrayOutputSteam(操作字节数组)

    直接看代码: package cn.itcast.io.p6.bytestream; import java.io.ByteArrayInputStream; import java.io.ByteA ...

  7. Java之ByteArrayInputStream和ByteArrayOutputStream-操作字节数组的类

    ByteArrayInputStream和ByteArrayOutputStream 源:内存中的字节数组 目的:内存中的字节数组 这两个流对象不涉及底层资源的调用,操作的都是内存中的数组,所以不需要 ...

  8. ByteArrayInputStream 和 ByteArrayOutputStream

    package java.io; /** * A <code>ByteArrayInputStream</code> contains * an internal buffer ...

  9. 黑马程序猿 IO流 ByteArrayInputStream与ByteArrayOutputStream

    ---------------------- ASP.Net+Unity开发..Net培训.期待与您交流! ---------------------- package cn.itcast.IO; i ...

  10. ByteArrayInputStream&ByteArrayOutputStream源码分析

    #ByteArrayInputStream 源码 ``` public synchronized int read(byte b[], int off, int len) { if (b == nul ...

随机推荐

  1. docker制作镜像

    使用Dockerfile脚本创建jdk1.8镜像 新建jdk目录:(-p表示需要父目录,不然就会出错) mkdir -p /usr/local/dockerjdk1. 将jdk的压缩文件复制到上面的路 ...

  2. 100-days: twenty-three

    Title: The surprising connection between single women and gentrification connection n.连接,联系,关系:连接点; ...

  3. 【python路飞】编码 ascii码(256位 =1个字节)美国;unicode(万国码)中文 一共9万个 用4个字节表示这9万个子 17位就能表示

    8位一个字节  1024字节 1KB   1024KB 1MB ASCII码不能包含中文.创建了unicode,一个中文4个字节.UTF-8一个中文3个.GBK中国人用的只包含中文2个字节 升级 Un ...

  4. Find out where to contain the smartforms

    Go to table E071 and give smarforms name and it will give the transport req for that. Run SE03, choo ...

  5. Scrapy Spider MiddleWare 设置

    # -*- coding: utf-8 -*- # Define here the models for your spider middleware # # See documentation in ...

  6. Visual Studio连接Oracle数据库

    一.安装Oracle Developer Tools for Visual Studio 2015 其他的什么client一概不要装,装了的直接卸载. 下载时需要登录,如果之前已经注册账号,提醒一下密 ...

  7. table 的部分使用,固定行,固定列等

    主要是用多张table表格实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. UVa10474

    #include <bits/stdc++.h> using namespace std; ; int main() { int n,q,x; ; int a[maxn]; while(c ...

  9. 设计模式学习心得<桥接模式 Bridge>

    说真的在此之前,几乎没有对于桥接模式的应用场景概念. 桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化.这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来 ...

  10. Object.defineProperty(obj,prop,descriptor)使用

    初步实现了数据自动映射到html中,动态修改对象数据也很自动更新到html.提供addProps方法-添加新增属性并初始化自动监听代码如下: 1.abserve.js:包含数据监听实现.类似jquer ...