Sunday, May 17, 2015

Make A Copy Of A Pdf File Using Java

package com.pkm.concat.two.pdf;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Created by pritom on 17/05/2015.
 */
public class FileToByteToFile {
    public static void main(String[] args) throws Exception {
        File file = new File("original.pdf");
        FileInputStream fileInputStream = new FileInputStream(file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        for (int readNum; (readNum = fileInputStream.read(bytes)) != -1;) {
            byteArrayOutputStream.write(bytes, 0, readNum);
        }
        byte[] bytesRead = byteArrayOutputStream.toByteArray();
        File outputFile = new File("Duplicate-" + file.getName().toLowerCase());
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        fileOutputStream.write(bytesRead);
        fileOutputStream.flush();
        fileOutputStream.close();
    }
}

No comments:

Post a Comment