FileInputStream类的使用初步

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

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字节。那么其中的多余字节就是由空格占位造成的。java

运行效果以下:数组

FileInputStream类的使用初步

显然咱们并不但愿看到这样的结果。那么如何解决?url

修改以下:spa

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字节以后的数据将没法读取。那么能不能按照文件大小构建字节数组呢?能!code

实例3:htm

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

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

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

实例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();//资源类操做的结尾必定要关闭。
}
}
相关文章
相关标签/搜索