Skip to main content

APIzation: Replication Package

[Q#16891792][A#16891978] How to read pdf file and write it to outputStream

https://stackoverflow.com/q/16891792

I need to read a pdf file with filepath "C:\file.pdf" and write it to outputStream. What is the easiest way to do that? ……………………………………………………………………………….

Answer

https://stackoverflow.com/a/16891978

The easiest way so far. Hope this helps.

APIzation

import java.io.*;


public class FileRead {


    public static void main(String[] args) throws IOException {


        File f=new File("C:\\Documents and Settings\\abc\\Desktop\\abc.pdf");

        OutputStream oos = new FileOutputStream("test.pdf");

        byte[] buf = new byte[8192];

        InputStream is = new FileInputStream(f);

        int c = 0;

        while ((c = is.read(buf, 0, buf.length)) > 0) {
            oos.write(buf, 0, c);
            oos.flush();
        }

        oos.close();
        System.out.println("stop");
        is.close();

    }

}
package com.stackoverflow.api;

import java.io.*;

public class Human16891978 {

  public static void copyFile(String srcFilePath, String str1)
    throws IOException {
    File f = new File(srcFilePath);

    OutputStream oos = new FileOutputStream(str1);

    byte[] buf = new byte[8192];

    InputStream is = new FileInputStream(f);

    int c = 0;

    while ((c = is.read(buf, 0, buf.length)) > 0) {
      oos.write(buf, 0, c);
      oos.flush();
    }

    oos.close();
    System.out.println("stop");
    is.close();
  }
}

package com.stackoverflow.api;

import java.io.*;

/**
 * How to read pdf file and write it to outputStream
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/16891978">https://stackoverflow.com/a/16891978</a>
 */
public class APIzator16891978 {

  public static void readFile(byte[] buf, String str1, String str2)
    throws IOException {
    File f = new File("C:\\Documents and Settings\\abc\\Desktop\\abc.pdf");
    OutputStream oos = new FileOutputStream(str1);
    InputStream is = new FileInputStream(f);
    int c = 0;
    while ((c = is.read(buf, 0, buf.length)) > 0) {
      oos.write(buf, 0, c);
      oos.flush();
    }
    oos.close();
    System.out.println("stop");
    is.close();
  }
}