java io学习

下图是java.io输入流的uml图

[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();
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值