FileInputStream类的使用初步

文件输入流java.io.FileInputStream类继承自字节输入流InputStream类,表示从文件中读取二进制数据到程序中:

public class FileInputStreamDemo01{
public static void main(String args[]) throws Exception{
//先向文件中写入数据,以便下面进行读取
File f1=new File("e:"+File.separator+"test.doc");
OutputStream out=new FileOutputStream(f1,true);
String message2="\r\n浙江师范大学行知学院";
byte[] b2=message2.getBytes();
for(int i=0;i<b2.length;i++){
out.write(b2[i]);
}
out.close();//数据写入完成,关闭输出流。
//下面开始从文件中读取数据
InputStream in=new FileInputStream(f1);
byte[] b=new byte[1024];//开辟了1024个字节的内存空间,不足就用空格补足
in.read(b);
System.out.println("读取内容如下:"+new String(b));
in.close();
}
}

因为开辟了1024字节的内存空间,但是写入的数据量显然没有1024字节。因此要是将这个1024大小的字节数组重新构建成字符串,这个字符串的长度或者说大小也将是1024字节。那么其中的多余字节就是由空格占位形成的。

运行效果如下:

FileInputStream类的使用初步

显然我们并不希望看到这样的结果。那么如何解决?

修改如下:

public class FileInputStreamDemo02{
public static void main(String args[]) throws Exception{
File f1=new File("e:"+File.separator+"test.doc");
OutputStream out=new FileOutputStream(f1,true);
String message2="\r\n浙江师范大学行知学院";
byte[] b2=message2.getBytes();
for(int i=0;i<b2.length;i++){
out.write(b2[i]);
}
out.close();
InputStream in=new FileInputStream(f1);
byte[] b=new byte[1024];//开辟1024空间大小的内存空间
int len=in.read(b);//len存储着返回读入缓冲区的字节总数
System.out.println("读取内容的字节总数:"+len);
//根据字节数组,构造从0开始的长度为len的新字符串
System.out.println("读取内容如下:"+new String(b,0,len));
in.close();
}
}

因为在重新构造字符串时是依据实际读取的字节大小构建的,因此新的字符串将不会有空格占位的情况。

上面的程序虽然解决了部分的问题,但在构建字节数组用于存储读取内容是开辟的内存空间是固定不变的,这样的设计极不合理。因为假如需要读取的数据量大于1024字节,这样1024字节之后的数据将无法读取。那么能不能按照文件大小构建字节数组呢?能!

实例3:

public class FileInputStreamDemo03{
public static void main(String args[]) throws Exception{
File f1=new File("e:"+File.separator+"test.doc");
OutputStream out=new FileOutputStream(f1,true);
String message2="\r\n浙江师范大学行知学院";
byte[] b2=message2.getBytes();
for(int i=0;i<b2.length;i++){
out.write(b2[i]);
}
out.close();
InputStream in=new FileInputStream(f1);
byte[] b=new byte[(int)f1.length()];//根据文件大小给字节数组分配内存空间
for(int i=0;i<b.length;i++){
b[i]=(byte)in.read();//每次只读取1个字节
}
System.out.println("读取内容如下:"+new String(b));
in.close();
}
}

这样的修改就能使内存资源得到最大限度的利用。

下面使用另一种读取数据的方式。

实例4:

public class FileInputStreamDemo04{
public static void main(String args[]) throws Exception{
File f1=new File("e:"+File.separator+"test.doc");
OutputStream out=new FileOutputStream(f1,true);
String message2="\r\n浙江师范大学行知学院";
byte[] b2=message2.getBytes();
for(int i=0;i<b2.length;i++){
out.write(b2[i]);
}
out.close();
InputStream in=new FileInputStream(f1);
byte[] b=new byte[(int)f1.length()];
int index=0;
int temp=0;
while((temp=in.read()) != -1){//表示没有读取到文件末尾。
b[index]=(byte)temp;
index++;
}
System.out.println("读取的文件内容:"+new String(b));
in.close();//资源类操作的结尾一定要关闭。
}
}

 

转载于:https://my.oschina.net/u/2331760/blog/678351

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值