`
lirig
  • 浏览: 235523 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java中write(byte[] b)与write(byte[] b,int off,int len)区别

    博客分类:
  • Jave
阅读更多

在项目中要做图片上传功能,由于用了struts2 文件上传方便了很多,在actoin中增加如下方法就可以了:

private static final int BUFFER_SIZE = 16 * 1024;

private static void copy(File src, File dst) {

        try {

            InputStream in = null;

            OutputStream ut = null;

            try {

                in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);

                ut = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);

                byte[] buffer = new byte[BUFFER_SIZE];

                while (in.read(buffer) > 0) {

                    out.write(buffer);

                }

            } catch (Exception e) {

                e.printStackTrace();

            } finally {

                if (null != in) {

                    in.close();

                }

                if (null != out) {

                    out.close();

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

 

文件上传很简单就实现了,可是突然我发现上传的文件都比原来大十几K.由于系统用户量很大,如果有一百万张,那占的空间可非常大了,查看jdk文档,其中在BufferedOutputStream文档中有write(byte[] b, int off, int len)和write(byte[] b).其中write(byte[ ] b,int off,int len)注释为:

       将指定 byte 数组中从偏移量off开始的len个字节写入此缓冲的输出流。一般来说,此方法将给定数组的字节存入此流的缓冲区中,根据需要将该缓冲区刷新,并转到底层输出流。但是,如果请求的长度至少与此流的缓冲区大小相同,则此方法将刷新该缓冲区并将各个字节直接写入底层输出流。因此多余的BufferedOutputStream将不必复制数据。

       果然我试了write(byte[ ] b,int off,int len)方法文件没有变大.由于习惯,在跟踪下去看看是究竟怎么回事.最后发现write(byte[ ]) 是调用了write(byte[] b,int off,int len),其中len数组的长度.问题就出在这里.在最后一次写入流时,len一般不会为读入字节的长度.除非文件大小刚好被BUFFER_SIZE整除.而通过 while ((len = in.read(buffer)) > 0){write(byte[] b,int off,int len) ,其中len为实际读入流的字节长度.所以这个方法不会增加文件大小,不会把多余的字节写进去.

       也不知道为什么sun在java中增加了write(byte[] b) 方法,不过该方法不会影响文件.当我用MagickImage处理一把,文件就恢复原样了.

       技术真要求精益求精,多思考,才能多领会.

 

10
3
分享到:
评论
6 楼 sghcel 2010-08-12  
以前传EXE文件的时候就发现了这个问题
5 楼 java_xh 2010-03-29  
哈哈,这个问题以前用Java切电影的时候发现了!
4 楼 lanmaokyle 2010-03-26  
受教了,又学到了一招。
3 楼 yjs200811 2010-03-22  
传方便了很多,
2 楼 mycybyb 2010-03-22  
晕,这么简单的问题。不好好反思自己的错误,居然怪到sun的头上了
1 楼 flyer2010 2010-03-21  
呵呵,你以前跟我们讲过这个。 while ((len = in.read(buffer)) > 0){write(byte[] b,int off,int len) 代替 write(byte[] b).

我们大部分的时候只是在用,都没有去读文档,刨根问底的精神不够。 哈哈,要向你学习。

相关推荐

    Student.java

    java集合类的文件,//Student s = new Student(); s = null;... void write(byte[] b,int off,int len) 写出某个范围的字节道数组b中 b 数据写出的数组 off 第一个写出字节在b中的偏移量

    用Java语言开发的网络蚂蚁,很实用哦

    public synchronized int write(byte[] b,int nStart,int len){ int n=-1; try{ rf.write(b,nStart,len); n=len; }catch(IOException ioe){ ioe.printStackTrace(); } return n; } public ...

    java中实现复制文件和文件夹

    java中实现复制文件和文件夹 public static void copyFile(File sourceFile,File targetFile) throws IOException{ // 新建文件输入流并对它进行缓冲 FileInputStream input = new FileInputStream(sourceFile)...

    Java中byte输出write到文件的实现方法讲解

    今天小编就为大家分享一篇关于Java中byte输出write到文件的实现方法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    Write Off

    Write Off

    Write Java program called AverageNumbers

    Write Java program called AverageNumbers.java to determine the average of several numbers. 

    java文件复制

    output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if(temp.isDirectory()){//如果是子文件夹 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); } } } ...

    Byte_Read_Write.rar_byte_read_write_三菱 通讯_三菱PLC_单片机 Byte_Read

    三菱FX系列PLC编程口通讯程序之字节读取(发布源码)

    JAVA-write-EXCEL.rar_copy excel java_java write_java读取_java读取Exc

    java读取Excel表格,拷贝、更新Excel工作薄

    TCP并发上传——java源码

    int len; ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); while (true) { len = in.read(buf); if (len == -1) { new TCPClient(bufOut.toByteArray(), InetAddress.getLocalHost(), 1211); ...

    javaIO流操作.rar

    //// byte[] b = new byte[1024]; //// while(in.read(b) != -1){ //// out.write(b); //// } // //操作流 法三 // byte[] b = new byte[1024]; // int length = 0; // while((length = in.read(b)...

    java实验4.3(SalesArray.java )

    Design and write a Java class named SalesArray.java that will: 1. Declare a two-dimensional integer array named sales. Populate the first four columns using the following data. Quarter 1 Quarter 2 ...

    关于文件复制的程序java

    int len; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e....

    java实现二维码生成

    import java.awt.Color;  import java.awt.Graphics2D;  import java.awt.image.BufferedImage;  import java.io.File;  import javax.imageio.ImageIO;  import com.swetake.util.Qrcode;  public class ...

    PEP人教版小学英语五年级下册Unit 5 B Read and write教学反思.pdf

    PEP人教版小学英语五年级下册Unit 5 B Read and write教学反思.pdf

    day019-io笔记和代码.rar

    void write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。 最常用 * //2. void write(byte[] b)将 b.length个字节从指定的字节数组写入此文件输出流。 ...

    Java文件处理工具类--FileUtil

    * Write binary byte array to local file. * * @param fileName - * local file name will write to * @param content * binary byte array * @return true if success * @throws IOException *...

    java 编写文件上传类简单易用

    用 Java 编写的、协议和平台都独立的服务器端组件,使用请求/响应的模式,提供了一个基于 Java 的服务器解决方案。本文将从文件传输的基本原理入手,分析如何用 java进行文件的上传,并提出解决方案。 一、基本...

    java算法,实现压缩及解压缩

    Java实现压缩与解压缩ZIP  import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;...

    Java中FileOutputStream流的write方法

    主要为大家详细介绍了Java中FileOutputStream流的write方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Global site tag (gtag.js) - Google Analytics