Created
October 29, 2022 16:33
-
-
Save rmg007/f39437a69fe2c893b2c9e3662c203966 to your computer and use it in GitHub Desktop.
ByteArrayInputStream ByteArrayOutputStream Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
public class ByteArrayInputStreamByteArrayOutputStreamExample { | |
static String filePath = "/Users/ryan/Desktop/file.txt"; | |
public static void main(String[] args) throws IOException { | |
String word = "java"; | |
byte[] bytes = word.getBytes(); | |
//try(ByteArrayInputStream bais = new ByteArrayInputStream(bytes)){ | |
// System.out.println(bais.available()); | |
// for (int i = 0; i < bytes.length; i++) { | |
// System.out.println((char) bais.read()); | |
// } | |
//} | |
try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
FileOutputStream fos = new FileOutputStream(filePath, true)){ | |
//System.out.println(baos.size()); | |
baos.write('a'); | |
baos.write('b'); | |
baos.write('c'); | |
baos.write('d'); | |
//System.out.println(baos.size()); | |
//System.out.println(baos); | |
baos.writeTo(fos); | |
baos.flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment