Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rmg007/f39437a69fe2c893b2c9e3662c203966 to your computer and use it in GitHub Desktop.
Save rmg007/f39437a69fe2c893b2c9e3662c203966 to your computer and use it in GitHub Desktop.
ByteArrayInputStream ByteArrayOutputStream Example
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