import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* ByteArrayInputStream 测试程序
*/
public class ByteArrayInputStreamTest {
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);
tesByteArrayInputStream() ;
}
/**
* ByteArrayInputStream的API测试函数
*/
private static void tesByteArrayInputStream() {
// 创建ByteArrayInputStream字节流,内容是ArrayLetters数组
ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);
// 从字节流中读取5个字节
for (int i=0; i<LEN; i++) {
// 若能继续读取下一个字节,则读取下一个字节
if (bais.available() >= 0) {
// 读取“字节流的下一个字节”
int tmp = bais.read();
System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));
}
}
// 若“该字节流”不支持标记功能,则直接退出
if (!bais.markSupported()) {
System.out.println("make not supported!");
return ;
}
// 标记“字节流中下一个被读取的位置”。即--标记“0x66”,因为因为前面已经读取了5个字节,所以下一个被读取的位置是第6个字节”
// (01), ByteArrayInputStream类的mark(0)函数中的“参数0”是没有实际意义的。
// (02), mark()与reset()是配套的,reset()会将“字节流中下一个被读取的位置”重置为“mark()中所保存的位置”
bais.mark(0);
// 跳过5个字节。跳过5个字节后,字节流中下一个被读取的值应该是“0x6B”。
bais.skip(5);
// 从字节流中读取5个数据。即读取“0x6B, 0x6C, 0x6D, 0x6E, 0x6F”
byte[] buf = new byte[LEN];
bais.read(buf, 0, LEN);
// 将buf转换为String字符串。“0x6B, 0x6C, 0x6D, 0x6E, 0x6F”对应字符是“klmno”
String str1 = new String(buf);
System.out.printf("str1=%s\n", str1);
// 重置“字节流”:即,将“字节流中下一个被读取的位置”重置到“mark()所标记的位置”,即0x66。
bais.reset();
// 从“重置后的字节流”中读取5个字节到buf中。即读取“0x66, 0x67, 0x68, 0x69, 0x6A”
bais.read(buf, 0, LEN);
// 将buf转换为String字符串。“0x66, 0x67, 0x68, 0x69, 0x6A”对应字符是“fghij”
String str2 = new String(buf);
System.out.printf("str2=%s\n", str2);
}
}
ArrayLetters=abcdefghijklmnopqrstuvwxyz 0 : 0x61 1 : 0x62 2 : 0x63 3 : 0x64 4 : 0x65 str1=klmno str2=fghij
import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
/**
* ByteArrayOutputStream 测试程序
*
* @author skywang
*/
public class ByteArrayOutputStreamTest {
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字节流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 依次写入“A”、“B”、“C”三个字母。0x41对应A,0x42对应B,0x43对应C。
baos.write(0x41);
baos.write(0x42);
baos.write(0x43);
System.out.printf("baos=%s\n", baos);
// 将ArrayLetters数组中从“3”开始的后5个字节写入到baos中。
// 即对应写入“0x64, 0x65, 0x66, 0x67, 0x68”,即“defgh”
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);
// 将baos写入到另一个输出流中
try {
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
baos.writeTo((OutputStream)baos2);
System.out.printf("baos2=%s\n", baos2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
baos=ABC baos=ABCdefgh size=8 str=ABCdefgh baos2=ABCdefgh
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有