How to read pdf file and write it to outputStream [closed]

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(); … Read more

Java – How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?

You should use Java’s built in serialization mechanism. To use it, you need to do the following: Declare the Club class as implementing Serializable: public class Club implements Serializable { … } This tells the JVM that the class can be serialized to a stream. You don’t have to implement any method, since this is … Read more

Is it necessary to close each nested OutputStream and Writer separately?

Assuming all the streams get created okay, yes, just closing bw is fine with those stream implementations; but that’s a big assumption. I’d use try-with-resources (tutorial) so that any issues constructing the subsequent streams that throw exceptions don’t leave the previous streams hanging, and so you don’t have to rely on the stream implementation having … Read more

Can you explain the HttpURLConnection connection process?

String message = URLEncoder.encode(“my message”, “UTF-8”); try { // instantiate the URL object with the target URL of the resource to // request URL url = new URL(“http://www.example.com/comment”); // instantiate the HttpURLConnection with the URL object – A new // connection is opened every time by calling the openConnection // method of the protocol handler … Read more