write(byte b[], int off, int len)与write(byte b[])
来源:互联网 发布:mac pro 需要的配件 编辑:程序博客网 时间:2024/06/05 08:23
在进行写文件的时候有时候返现,通过write(byte b[])方式写文件比原来的文件大一些。流程代码:
public static void main(String[] args) throws Exception { long t1 = System.currentTimeMillis(); File file = new File("d:/11.jpg"); FileInputStream in = new FileInputStream(file); BufferedInputStream brin = new BufferedInputStream(in, 1024); File outFile = new File("d:/112.jpg"); FileOutputStream out = new FileOutputStream(outFile); BufferedOutputStream bout = new BufferedOutputStream(out); byte input[] = new byte[1024]; while ((brin.read(input))!= -1) { bout.write(input); } bout.close(); out.flush(); out.close(); System.out.println((System.currentTimeMillis() - t1)); }看了下write(byte b[])的实现是
public void write(byte b[]) throws IOException { write(b, 0, b.length); }当在while循环里面写文件时,在最后一次如果剩余是562个byte;由于读取暂存到的byte是1024大小,write(byte b[])底层实现是偏移量是0,大小是byte的长度,所以最后一次写及时562任然写入1024。所以在最终写入的结果里面是比原始图片要大一些。那么需要改为,读到多少写多少。
public static void main(String[] args) throws Exception { long t1 = System.currentTimeMillis(); File file = new File("d:/餐巾纸.jpg"); FileInputStream in = new FileInputStream(file); BufferedInputStream brin = new BufferedInputStream(in, 1024*5); File outFile = new File("d:/餐巾纸2.jpg"); FileOutputStream out = new FileOutputStream(outFile); BufferedOutputStream bout = new BufferedOutputStream(out); byte input[] = new byte[1024*5]; int count; while ((count=brin.read(input))!= -1) { bout.write(input,0,count); } bout.close(); out.flush(); out.close(); System.out.println((System.currentTimeMillis() - t1)); }总结:
write(byte b[], int off, int len)表示:
b 这一次写的数据
off 这次从b的第off开始写
len 这次写的长度。
public int read(byte[] b, int off, int len)
b -- 目标字节数组。
off -- 将读取的数据写入到数组b中,off表示从数据b的哪个位置开始写。
len -- 要读取的字节数。
IOException -- 如果发生I/ O错误。
NullPointerException -- 如果b为 null.
IndexOutOfBoundsException -- 如果off为负,len为负,或len大于b.length - off。
阅读全文
0 0
- java中write(byte[] b)与write(byte[] b,int off,int len)区别
- java中write(byte[] b)与write(byte[] b,int off,int len)区别
- java中write(byte[] b)与write(byte[] b,int off,int len)区别
- write(byte b[], int off, int len)与write(byte b[])
- write(byte b[], int off, int len)详解
- 浅谈write(byte[] b)和write(byte[] b,int off,int len)的区别
- java.io.OutputStream.write(byte[] b, int off, int len)方法实例
- InputStream.read(byte[] b,int off,int len)
- java.io.InputStream.read(byte[] b, int off, int len)
- InputStream.read(byte[] b,int off,int len)
- IO流里的 read(),read(byte[]),read(byte[],int off,int len)以及write
- Java当中InputStream中read()、read(byte[] b)、read(byte[] b,int off,int len)
- InputStream当中read()、read(byte[] b)、read(byte[] b、int off 、int len)的使用
- 教你完全理解IO流里的 read(),read(byte[]),read(byte[],int off,int len)以及write
- 教你完全理解IO流里的 read(),read(byte[]),read(byte[],int off,int len)以及write
- 基于java的InputStream.read(byte[] b,int off,int len)算法学习!
- 基于java的InputStream.read(byte[] b,int off,int len)算法学习
- 基于java的InputStream.read(byte[] b,int off,int len)算法学习
- Oracle ASH内存强制Flush日志解决一例
- 这些支付的负面记录,分分钟影响你的贷款
- hibernate多对一映射
- dos命令中oracle常用设置
- PHP获取上周、本周、上月、本月、本季度、上季度时间方法大全
- write(byte b[], int off, int len)与write(byte b[])
- input动态下拉--ajax
- React学习小结
- dubbo 2.5.4 在JDK8下启动错误
- JAVA验证码生成器
- 怎么将自己的工程,共享到svn?
- EditText默认不弹出软件键盘
- MySQL 的介绍
- 自底向上分析网络体系结构