Java架构师之旅(三十五 基础IO流)

夜光序言:

每个人,总会因为遗憾而记得一个人~~

正文:

我们要多写写嗯~

 

IO流→其他字节流,字节数组流等~~

  • 字节数组 字节 节点流
  • 输入流ByteArrayInputStream
  • 输出流 ByteArrayOutputStream

package com.Genius.others;

 

import java.io.BufferedInputStream;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

 

/*夜光:字节数组节点流

 * 数组的长度有限,那就表示不会很大

 *

 * */

public class ByteArrayDemo01 {

 

public static void main(String[] args) throws IOException {

// 我们应该学会思考每一点

read();

 

}

 

//下面这个是输入流,操作与文件输入流一致

public static void read() throws IOException{

//数据源

String msg ="夜光:看得到呢……在黑暗中,闪着光芒……简直就像是";

byte[] src = msg.getBytes();

 

//选择流

InputStream is = new BufferedInputStream(

       new ByteArrayInputStream(

        src

        )

);

//下面操作

byte[] flush = new byte[1024];

int len = 361;

while(-1!=(len=is.read(flush))){

System.out.println(new String(flush,0,len));

}

 

//之后就是释放资源

is.close();

 

}

 

}

 

还有一种方法~~

package com.Genius.others;

 

import java.io.BufferedInputStream;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

 

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

 

/*夜光:字节数组节点流

 * 数组的长度有限,那就表示不会很大

 *

 * */

public class ByteArrayDemo01 {

 

public static void main(String[] args) throws IOException {

// 我们应该学会思考每一点

read(write());

 

}

 

//输出流,与文件输出流不同,因为有新增方法,不可以使用多态~~

public static byte[] write() throws IOException{

//目的地

byte[] dest;

//选择流

ByteArrayOutputStream bos = new ByteArrayOutputStream();

//操作 写出

String msg ="夜光:看得到呢……在黑暗中,闪着光芒……简直就像是";

byte[] info = msg.getBytes();

bos.write(info,0,info.length);

// 获取数据

dest = bos.toByteArray();

// 之后释放资源

bos.close();

return dest;

 

 

}

 

 

 

 

 

 

//下面这个是输入流,操作与文件输入流一致

public static void read(byte[] src) throws IOException{

 

 

//选择流

InputStream is = new BufferedInputStream(

       new ByteArrayInputStream(

        src

        )

);

//下面操作

byte[] flush = new byte[1024];

int len = 361;

while(-1!=(len=is.read(flush))){

System.out.println(new String(flush,0,len));

}

 

//之后就是释放资源

is.close();

 

}

 

}

猜你喜欢

转载自blog.csdn.net/weixin_41987706/article/details/86589936
今日推荐