Java字节数组输入流ByteArrayInputStream
发布日期:2021-05-07 03:00:32 浏览次数:15 分类:原创文章

本文共 762 字,大约阅读时间需要 2 分钟。

package cn.sxt.test;import java.io.ByteArrayInputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;/** * 测试文件字节数组输入流 * @author 佳哥 * */public class TestReader {   public static void main(String[] args) {   	//创建源	byte[] b = "wo jiao jiage".getBytes();		//选择流	InputStream is = null;	try	{   		is = new ByteArrayInputStream(b);		//操作		byte[] flush = new byte[20];//缓冲数组,暂时存放文本的内容		int len = -1;//获取文本的长度		while((len = is.read(flush))!=-1)		{   			String str = new String(flush,0,len);//把文本的内容由字符数组转化为字符串形式输出			System.out.println(str);		}			}catch(IOException e){   		e.printStackTrace();	}finally	{   	//无需关闭流。此部分由gc处理	/*	if(is != null)		{			try {				is.close();			} catch (IOException e) {								e.printStackTrace();			}		}		*/	}	}}
上一篇:Java对接流
下一篇:Java文件字符复制Reader和Writer的综合

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年03月28日 05时31分07秒