下图是java.io输入流的uml图
[img]http://dl.iteye.com/upload/attachment/366730/6c331c4b-f45d-3a8e-9f9c-11340ddd607e.png[/img]
InputStream是表示字节输入流的所有类的超类。
需要定义 InputStream 子类的应用程序必须总是提供返回下一个输入字节的方法。
ByteArrayInputStream类使用
[img]http://dl.iteye.com/upload/attachment/366730/6c331c4b-f45d-3a8e-9f9c-11340ddd607e.png[/img]
InputStream是表示字节输入流的所有类的超类。
需要定义 InputStream 子类的应用程序必须总是提供返回下一个输入字节的方法。
ByteArrayInputStream类使用
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class ByteArrayTest {
public static void main(String[] args) {
try {
File file = new File(System.getProperty("user.dir")+File.separator+"test.txt");
BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream arrayOutPutStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1];
while(buffer.read(bytes)!=-1)
{
arrayOutPutStream.write(bytes);
}
arrayOutPutStream.close();
buffer.close();
bytes = arrayOutPutStream.toByteArray();
for (byte b : bytes) {
System.out.print((char)b);
}
System.out.println();
Scanner scanner = new Scanner(System.in);
System.out.print("输入修改的位置:");
int pos = scanner.nextInt();
System.out.print("输入修改的字符:");
bytes[pos-1]=(byte)scanner.next().charAt(0);
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
System.out.println("测试此 InputStream 是否支持 mark/reset:"+arrayInputStream.markSupported());
BufferedOutputStream bufferOutput = new BufferedOutputStream(new FileOutputStream(file));
byte temp[] = new byte[1];
while(arrayInputStream.read(temp)!=-1)
{
bufferOutput.write(temp);
}
arrayInputStream.close();
bufferOutput.flush();
bufferOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}