首页 > 编程语言 > 详细

使用Java FileInputStream读取文件内容到字节数组中

时间:2015-08-03 11:40:43      阅读:923      评论:0      收藏:0      [点我收藏+]
package date0802;

import java.io.FileInputStream;
import java.io.IOException;

public class InputStream {

	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		FileInputStream fileInputStream = new FileInputStream("1.txt");
		//获取文件大小字节
		int length=fileInputStream.available();
		
		//读取文件字节到一个数组中
		int bytesRead=0;
		int bytesToRead=length;
		byte[] input=new byte[bytesToRead];
		while(bytesRead<bytesToRead) {
			int result=fileInputStream.read(input,bytesRead,bytesToRead-bytesRead);
			if(result==-1)
				break;
			bytesRead+=result;
		}
                fileInputStream.close();
		System.out.println((bytesRead==length));
	}
}


使用Java FileInputStream读取文件内容到字节数组中

原文:http://my.oschina.net/zzw922cn/blog/486990

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!