Jva IO — ByteArray

ByteArrayInputStream:

public class ByteArrayIntputStreamTest {private static final int LEN = 5;// 对应英文字母“abcddefghijklmnopqrsttuvwxyz”,下面位各字母对应的地址。private static final byte[] ArrayLetters = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A};public static void main(String[] args) {String tmp = new String(ArrayLetters);System.out.println("ArrayLetters="+tmp);		//输出ArrayLetters代表的字母。tesByteArrayInputStream() ;			//ByteArrayInputStream 测试函数}/*** ByteArrayInputStream的API测试函数*/private static void tesByteArrayInputStream() {// 创建ByteArrayInputStream字节流,内容是ArrayLetters数组ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);System.out.println("当前字节流中剩余字节数: " + bais.available());System.out.println("从此输入流中读取下一个数据字节 : " + bais.read());//len从该输入流中将最多数据字节读入一个字节数组。/** b - 读取数据的缓冲区。* off - 目标数组中的起始偏移量 b* len - 读取的最大字节数。* */byte[] buf = new byte[LEN];System.out.println(bais.read(buf, 0, 3));String str1 = new String(buf);System.out.printf("str1=%s\n", str1);//测试是否InputStream支持标记/重置。	--- public boolean markSupported()if (!bais.markSupported()) {System.out.println("make not supported!");return ;}//设置流中当前标记的位置。    	--- public void mark(int readAheadLimit)bais.mark(1);//跳过n此输入流的输入字节。bais.skip(5);// 重置“字节流”:即,将“字节流中下一个被读取的位置”重置到“mark()所标记的位置”,即0x66。bais.reset();// 从“重置后的字节流”中读取5个字节到buf中。即读取“0x66, 0x67, 0x68, 0x69, 0x6A”//bais.read(buf, 0, LEN);bais.read(buf,2,3);// 将buf转换为String字符串。“0x66, 0x67, 0x68, 0x69, 0x6A”对应字符是“fghij”String str2 = new String(buf);System.out.printf("str2=%s\n", str2);}
}

ByteArrayOutputStream:

/*** ByteArrayOutputStream 测试程序** @author skywang*/
public class  a{private static final int LEN = 5;// 对应英文字母“abcddefghijklmnopqrsttuvwxyz”private static final byte[] ArrayLetters = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A};public static void main(String[] args) {String tmp = new String(ArrayLetters);System.out.println("ArrayLetters="+tmp);tesByteArrayOutputStream() ;}/*** ByteArrayOutputStream的API测试函数*/private static void tesByteArrayOutputStream() {ByteArrayOutputStream baos = new ByteArrayOutputStream();// 将指定的字节写入此ByteArrayOutputStream。	--- public void write(int b)baos.write(97);baos.write(98);baos.write(99);System.out.println(baos);//写入len从指定的字节数组以偏移字节off此ByteArrayOutputStream。	--- public void write(byte [] b, int off, int len)baos.write(ArrayLetters, 3, 5);System.out.printf("baos=%s\n", baos);//返回缓冲区的当前大小。int size = baos.size();System.out.printf("size=%s\n", size);// 转换成byte[]数组	byte[] buf = baos.toByteArray();String str = new String(buf);System.out.printf("str=%s\n", str);//使用平台的默认字符集将缓冲区的内容转换为字符串解码字节。System.out.println(baos.toString());//将baos写入到另一个输出流中//public void writeTo(OutputStream  out)try {ByteArrayOutputStream baos2 = new ByteArrayOutputStream();System.out.println(baos2);baos.writeTo((OutputStream)baos2);System.out.printf("baos2=%s\n", baos2);} catch (IOException e) {e.printStackTrace();}}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部