您的位置:首页 > 移动开发 > Android开发

java4Android(15)字节流大文件读写,字符流读写

2015-06-23 08:17 531 查看
不能将很大数据内容一次性读取到byte数组中,我们需要循环几次将数据写入到目的文件中

例子:大文件字节流读写,输入输出流的关闭,try、catch用法

[code]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();
            }
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: