java写大文件

package cn.test.writefile;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: wulong
 * Date: 14-3-27
 * Time: 下午8:00
 * To change this template use File | Settings | File Templates.
 */
public class Test {


    public static void main(String[] args) {

        BufferedInputStream in = null;
        BufferedOutputStream out = null;

        ByteArrayOutputStream saveArray = new ByteArrayOutputStream();
        try {
            byte[] buf = new byte[1024 * 2];
            FileInputStream instream = new FileInputStream("/home/wulong/testp.pdf");
            in = new BufferedInputStream(instream);

            int len = 0;
            while ((len = in.read(buf)) != -1) {
                saveArray.write(buf,0,len);
            }

            out = new BufferedOutputStream(new FileOutputStream("/home/wulong/save.pdf"));
            out.write(saveArray.toByteArray());
            out.flush();

            System.out.println(saveArray.toByteArray().length);
            if (in != null) {
                in.close();
            }

            if (out != null) {
                out.close();
            }

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally {
        }
    }
}




你可能感兴趣的:(java写大文件)