java4Android(15)字节流大文件读写,字符流读写
·
不能将很大数据内容一次性读取到byte数组中,我们需要循环几次将数据写入到目的文件中
例子:大文件字节流读写,输入输出流的关闭,try、catch用法
import java.io.*;
class test
{
public static void main(String[] args)
{
//声明输入流引用,字节输入常用类FileInputStream
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
//生成输入流的对象
fis = new FileInputStream("D:/BaiduYunDownload/Java4Android/lx_1/from.txt");
fos = new FileOutputStream("D:/BaiduYunDownload/Java4Android/lx_1/to.txt");
//生成一个字节数组,每次读取1024个字节,文件大小是5MB,大概读五次
byte[] b = new byte[1024];
while(true)
{
//将文件中的数据读进程序
int temp = fis.read(b,0,b.length);
if(temp == -1)
{
break;
}
fos.write(b,0,temp);
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
try
{
//关闭字节流输入输出管道
fis.close();
fos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}更多推荐
所有评论(0)