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

What is the use case for null(Input/Output)Stream API in Java?

Sometimes you want to have a parameter of InputStream type, but also to be able to choose not to feed your code with any data. In tests it’s probably easier to mock it but in production you may choose to bind null input instead of scattering your code with ifs and flags. compare: class ComposableReprinter … Read more

How can I convert an InputStream to a DataHandler?

An implementation of the answer from Kathy Van Stone: At first, create a helper class, which creates a DataSource from an InputStream: public class InputStreamDataSource implements DataSource { private InputStream inputStream; public InputStreamDataSource(InputStream inputStream) { this.inputStream = inputStream; } @Override public InputStream getInputStream() throws IOException { return inputStream; } @Override public OutputStream getOutputStream() throws IOException … Read more

How can I get MIME type of an InputStream of a file that is being uploaded?

I wrote my own content-type detector for a byte[] because the libraries above weren’t suitable or I didn’t have access to them. Hopefully this helps someone out. // retrieve file as byte[] byte[] b = odHit.retrieve( “” ); // copy top 32 bytes and pass to the guessMimeType(byte[]) funciton byte[] topOfStream = new byte[32]; System.arraycopy(b, … Read more

Process.waitFor(), threads, and InputStreams

If your external process expects something on its stdin, you MUST close the getOutputStream. Otherwise you will waitFor forever. Here is the When Runtime.exec() won’t article from JavaWorld which describes different pitfalls of exec method and how to avoid them. From my experience it’s better to consume STDOUT and STDERR of child process ( until … Read more

How to log request inputstream with HttpModule, then reset InputStream position

I’ve worked out the problem: I think that calling dispose on the StreamReader must be killing the InputStream too. Instead of using the StreamReader I did the following: var bytes = new byte[request.InputStream.Length]; request.InputStream.Read(bytes, 0, bytes.Length); request.InputStream.Position = 0; string content = Encoding.ASCII.GetString(bytes); So the complete code: public class LoggingModule : IHttpModule { public void … Read more

Read line from an InputStream [duplicate]

You should use BufferedReader with FileInputStreamReader if your read from a file BufferedReader reader = new BufferedReader(new FileInputStreamReader(pathToFile)); or with InputStreamReader if you read from any other InputStream BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); Then use its readLine() method in a loop while(reader.ready()) { String line = reader.readLine(); } But if you really love InputStream … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)