Want to convert an OutputStream to a Writer in Java? Use the following Java program to convert an OutputStream to a Writer using Java programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.*; public class OutputStreamToWriterTest { public static void main(String[] args) throws Exception { String str = "TUTORIALSPOINT"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(baos); for (int i=0; i < str.length(); i++) { osw.write((int) str.charAt(i)); } osw.close(); byte[] b = baos.toByteArray(); for (int j=0; j < b.length; j++) { System.out.println(b[j]); } } } |
Program Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
84 85 84 79 82 73 65 76 83 80 79 73 78 84 |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.