byte[] bs = new byte[1024]问题

本文通过一个简单的字节读取示例介绍了如何在Java中逐块读取文件,并深入浅出地讲解了二进制地址线与存储单元数量之间的关系,帮助初学者更好地理解计算机内存的基本概念。
    byte[] bs = new byte[1024];
    int i = 0;
    while((i=in.read(bs))!=-1){
        out.write(bs, 0, i);
    }
    //每次以bs的大小读取文件,也就是1024B=1kB,

/*
初学者有时不容易开窍,我来做个启蒙吧
设想一个存储器,只有一条地址线A0,那么这个存储器只能有2个单元,A=0时访问一个单元,A=1
时访问另一个单元;
两条地址线(A1,A0),可以寻址4个单元的存储器:
(A1, A0) = (0, 0), (0, 1), (1, 0), (1, 1)
3条地址线(A2, A1,A0),可以寻址8个单元的存储器;
依此类推,地址线数目与存储单元个数的关系就很清楚了: 地址线数目 存储单元数 1 2| 2 4 |3 8 |4 16| 5 32| 6 64| 7 128 …. …. 10
1024 = 1K …. …. n 2^
*/

import java.io.IOException; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.BufferedInputStream; public class fileDemo { public static void main(String[] args) throws IOException { method1(); method2(); method3(); method4(); } // 一次读取一个字节 public static void method1() throws IOException { FileInputStream fis=new FileInputStream("D:\\bbb.mp4"); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; while((b=fis.read())!=-1) { //System.out.println((char)b); fos.write((char)b); } long end1 = System.currentTimeMillis(); System.out.println("使用FileInputStream的read()方法复制时间:" + (end1 - start1) + "ms"); fis.close(); fos.close(); } // 一次读取一个字节数组 public static void method2() throws IOException { FileInputStream fis=new FileInputStream("D:\\bbb.mp4"); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; byte[] bs=new byte[1024]; while((b=fis.read(bs))!=-1) { //System.out.println(new String(bs)); fos.write(bs,0,b); } long end1 = System.currentTimeMillis(); System.out.println("使用FileInputStream的readread(byte[] b)方法复制时间:" + (end1 - start1) + "ms"); fis.close(); fos.close(); } // 缓冲流+一次读一个字节 public static void method3() throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\bbb.mp4")); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; while((b=bis.read())!=-1) { //System.out.println((char)(b)); fos.write((char)b); } long end1 = System.currentTimeMillis(); System.out.println("使用BufferedInputStream的read()方法复制时间:" + (end1 - start1) + "ms"); bis.close(); fos.close(); } //缓冲流+一次读一个字节数组 public static void method4() throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\bbb.mp4")); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int len=0; byte[] bs=new byte[1024]; while((len=bis.read(bs))!=-1) { //System.out.println(new String(bs,0,len)); fos.write(bs,0,len); } long end1 = System.currentTimeMillis(); System.out.println("使用BufferedInputStream的read(byet[] b)方法复制时间:" + (end1 - start1) + "ms"); bis.close(); fos.close(); } } 给出每一行代码的注释
05-24
public File getFileByUrl(String downloadUrl) { InputStream is = null; FileOutputStream os = null; log.info("开始下载附件================"); try { //构造URL URL url = new URL(downloadUrl); log.debug("附件下载地址:" + downloadUrl); //打开连接 URLConnection con = url.openConnection(); is = con.getInputStream(); String baseFilePath = fileStorageProperties.getLocal().get(0).getBasePath(); log.debug("baseFilePath:" + downloadUrl); Path path = Paths.get(baseFilePath); log.debug("path:" + downloadUrl); // 判断目录是否存在 if (Files.exists(path) && Files.isDirectory(path)) { System.out.println("目录已经存在"); } else { try { // 如果目录不存在则创建目录 Files.createDirectories(path); } catch (IOException e) { throw new RtServerException("创建临时目录报错。"); } } File tempFile = File.createTempFile("temp", "tmp", new File(baseFilePath)); log.debug("tempFile-Path:" + baseFilePath); //1K的数据缓冲 byte[] bs = new byte[1024]; //读取到的长度 int len; os = new FileOutputStream(tempFile, true); //开始读取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } return tempFile; } catch (Exception e) { log.debug("下载文件异常," + e.getMessage()); log.error(e.getMessage()); e.printStackTrace(); throw new RtServerException(e.getMessage()); } finally { try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); throw new RtServerException(e.getMessage()); } try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); throw new RtServerException(e.getMessage()); } } }帮我添加删除临时文件的逻辑
08-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值