javabufferedreaderbufferedwriter

What is the getResourceAsStream() used only for reding inputs, how to write outputs


So basically I have a file that the java code reads from and writes to.

BufferedReader works

BufferedReader bReader = new BufferedReader(
            new InputStreamReader(
                    getClass().getClassLoader().getResourceAsStream(fileName)
            )
);

however, BufferedWriter doesn't work:

BufferedWriter bWrite = new BufferedWriter(
                new OutputStreamWriter(
                    getClass().getClassLoader().getResourceAsStream(fileName)
                )
);

'OutputStreamWriter(java.io.OutputStream)' in 'java.io.OutputStreamWriter' cannot be applied to '(java.io.InputStream)'


Solution

  • Well, one reads (input) and one writes (output) and since getResourceAsStream returns an InputStream it does not make sense to pass it into a Writer because you cannot write to an InputStream, you can only read from it.