不能将很大数据内容一次性读取到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();
            }
        }
    }
}
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐