Java使用字节流读取数据

输入流

public static void main(String[] args) {

        try {
            //输入流,用来读取数据
            FileInputStream fis=new FileInputStream("text.txt");
            byte input[]=new byte[40];//创建字节数组
            fis.read(input);//将数据读取到input 中
            String str=new String(input,"utf-8");
            System.out.println(str);
            fis.close();//关闭




        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

输出流

public static void main(String[] args) {
         try {
             //输出流
            FileOutputStream fos=new FileOutputStream("textw.txt");
            String str="wq123455";
            byte[]out=str.getBytes("utf-8");
            fos.write(out);//写入数据

            fos.close();//关闭
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
  ;

通过字节流拷贝文件(gif动画)

public static void main(String[] args) {
    try {
        FileInputStream fis=new FileInputStream("a.gif");
        FileOutputStream fos=new FileOutputStream("a_new.gif");
        byte  input[]=new byte[30];//字节数组

        while (fis.read(input)!=-1) {//读取到input字节数组中

        fos.write(input);//将数组写入到新的文件

    }

        fis.close();
        fos.close();
        System.out.println("done");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

带缓冲的字节流读取数据,BufferedOutputStream和BufferedInputStream

public static void main(String[] args) {
    try {


        FileInputStream fis=new FileInputStream("mps.mp4");
        BufferedInputStream bis=new BufferedInputStream(fis,1000000);//缓冲输入流,1000=1kb,缓冲区大小
        FileOutputStream fos=new FileOutputStream("mas_new.mp4");
        BufferedOutputStream bos=new BufferedOutputStream(fos,1000000);//缓存输出流
        //大型文件对应的数组可以大些,小型文件就小些
        byte input[]=new byte[100000];
        long before=System.currentTimeMillis();
        int count = 0;//定义个变量
        while (bis.read(input)!=-1) {//读取的数据等于-1代表读完,跳出循环
            bos.write(input);
            count++;

        }
        bis.close();//后打开的先关闭
        fis.close();//先打开的后关闭
        bos.close();
        fos.close();
        long time=System.currentTimeMillis()-before;//读取耗时的时间
        System.out.println("耗时"+time+"mm");
    System.out.println("共读取了"+count+"次");



    } catch (FileNotFoundException e) {     
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值