我是一个Android新手,我现在想读取一个文件,文件大小未知,我想用
byte[] buffer = new byte[3072];
temp_stream = new FileInputStream(img_name);
temp_stream.read(buffer);
buffer接收,如何声明一个动态byte[] buffer。
Java获得文件大小的方法(通过FileInputStream)
根据指定文件创建FileInputStream,调用available方法返回文件大小,容量为byte
参考FileInputStream/FileOutputStream的应用 ,提供了示例
import java.io.*;
public class FileStreamDemo {
public static void main(String[] args) throws IOException {
//创建两个文件,face.gif是已经存在文件,newFace.gif是新创建的文件
File inFile = new File("face.gif");
File outFile = new File("newFace.gif");
//创建流文件读入与写出类
FileInputStream inStream = new FileInputStream(inFile);
FileOutputStream outStream = new FileOutputStream(outFile);
//通过available方法取得流的最大字符数
byte[] inOutb = new byte[inStream.available()];
inStream.read(inOutb); //读入流,保存在byte数组
outStream.write(inOutb); //写出流,保存在文件newFace.gif中
inStream.close();
outStream.close();
}
byte[] buffer=new byte[1024];
int lens=0;
while((lens==temp_stream.read(buffer))!=-1){
}
这样就读出来了,这个是按照文件的大小读取的,
new FileInputStream(img_name)后就可以知道文件大小了。
先把文件读到流里面,这样就知道长度了,然后再new数组
java中没有动态数组的概念。
如何声明一个动态byte[] buffer。声明的一个内存流对象就可以了。