您的位置:首页 > 编程语言 > Java开发

java基础------存取大文件数据

2015-07-04 10:53 537 查看
实现原理:通过while循环分次读取数据

生成输入流对象FileInputStream();
生成输出流对象FileOutPutStream();
生成字节字数数组Btye[1024];
通过While循环每次read字节数组的最大值,当read返回值是-1的时候,说明读取完毕,跳出循环break
通过write写出数据(需要在read的同时写出数据,write的执行应该在read之后,且循环结束前)

因为read的返回值为每次读入数据的长度,所以write写入数据的长度为read的长度

public class Test extends Student {

public static void main(String[] args) throws IOException {
FileInputStream fileInputStream=new FileInputStream("D:\\test.zip");
FileOutputStream fileOutputStream=new FileOutputStream("D:\\test1.zip");

try{
//创建缓存区大小
byte[] buffer =new byte[1024];
while(true){
//创建一个临时存储区
int temp=fileInputStream.read(buffer,0,buffer.length);
if(temp==-1){
break;
}
//写出文件
fileOutputStream.write(buffer,0,temp);
}
}catch (Exception e){
e.printStackTrace();
}finally {
fileInputStream.close();
fileOutputStream.close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: