我是一个Android新手,我现在想读取一个文件,文件大小未知,我想用
byte[] buffer = new byte[3072];
temp_stream = new FileInputStream(img_name);
temp_stream.read(buffer);
buffer接收,如何声明一个动态byte[] buffer。
android开发如何声明动态一维数组
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
6条回答 默认 最新
- save4me 2015-01-12 01:33关注
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(); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报