ByteArrayOutputStream baos = new ByteArrayOutputStream(); // writing string to ByteArrayOutputStream String data = "Hello World"; baos.write(data.getBytes()); // converting ByteArrayOutputStream to String and printing it String result = new String(baos.toByteArray()); System.out.println(result);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // writing bytes to ByteArrayOutputStream byte[] data = {1, 2, 3}; baos.write(data); // converting ByteArrayOutputStream to byte array and printing it byte[] result = baos.toByteArray(); System.out.println(Arrays.toString(result));Package library: java.base