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

本文通过一个简单的字节读取示例介绍了如何在Java中逐块读取文件,并深入浅出地讲解了二进制地址线与存储单元数量之间的关系,帮助初学者更好地理解计算机内存的基本概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    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
package com.de.debook.utils; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; public class FileUploadUtils { /** * @param * @description: 生成一个唯一的ID * @return: java.lang.String */ public static String createUUID() { String s = UUID.randomUUID().toString(); String s2 = s.substring(24).replace("-", ""); return s2.toUpperCase(); } /** * @description: 得到一个保证不重复的临时文件名 * @return: java.lang.String */ public static String createTmpFileName(String suffix) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss"); String datestr = sdf.format(new Date()); String name = datestr + "-" + createUUID() + "." + suffix; return name; } /** * @param fileName 原始文件名 * @description: 得到文件的后缀名 * @return: java.lang.String */ public static String fileSuffix(String fileName) { int p = fileName.lastIndexOf('.'); if (p >= 0) { return fileName.substring(p + 1).toLowerCase(); } return ""; } public static void deleteDir(String dirPath) { File file = new File(dirPath); if (file.isFile()) { file.delete(); } else { File[] files = file.listFiles(); if (files == null) { file.delete(); } else { for (int i = 0; i < files.length; i++) { deleteDir(files[i].getAbsolutePath()); } file.delete(); } } } public static byte[] getFileByteArray(File file) { long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { System.out.println("file too big..."); return null; } byte[] buffer = null; try (FileInputStream fi = new FileInputStream(file)) { buffer = new byte[(int) fileSize]; int offset = 0; int numRead = 0; while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { offset += numRead; } // 确保所有数据均被读取 if (offset != buffer.length) { throw new IOException("Could not completely read file" + file.getName()); } } catch (Exception e) { e.printStackTrace(); } return buffer; } public static void writeBytesToFile(byte[] bs, String file) throws IOException { OutputStream out = new FileOutputStream(file); InputStream is = new ByteArrayInputStream(bs); byte[] buff = new byte[1024]; int len = 0; while ((len = is.read(buff)) != -1) { out.write(buff, 0, len); } is.close(); out.close(); } }
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值