Java : BufferedOutputStream con ejemplos
BufferedOutputStream (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de BufferedOutputStream.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var file = Path.of("R:", "java-work", "aaa.data");
System.out.println(file); // R:\java-work\aaa.data
try (final var os = new BufferedOutputStream(Files.newOutputStream(file))) {
os.write(10);
os.write(20);
os.write(30);
}
final var ret = Files.readAllBytes(file);
System.out.println(Arrays.toString(ret)); // [10, 20, 30]
Fields
protected byte[] buf
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
protected int count
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Fields declared in FilterOutputStream
Constructors
BufferedOutputStream (OutputStream out)
final var out = new ByteArrayOutputStream();
try (final var os = new BufferedOutputStream(out)) {
os.write(10);
os.write(20);
os.write(30);
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
BufferedOutputStream (OutputStream out, int size)
class MyOutputStream extends BufferedOutputStream {
MyOutputStream(OutputStream out) {
super(out);
}
MyOutputStream(OutputStream out, int size) {
super(out, size);
}
int getBufferSize() {
return buf.length;
}
}
try (final var os = new MyOutputStream(new ByteArrayOutputStream())) {
System.out.println(os.getBufferSize()); // 8192
}
try (final var os = new MyOutputStream(new ByteArrayOutputStream(), 32)) {
System.out.println(os.getBufferSize()); // 32
}
Methods
void flush ()
final var out = new ByteArrayOutputStream();
try (final var os = new BufferedOutputStream(out)) {
os.write(10);
System.out.println(Arrays.toString(out.toByteArray())); // []
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10]
os.write(20);
System.out.println(Arrays.toString(out.toByteArray())); // [10]
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
os.write(30);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
void write (byte[] b, int off, int len)
final var out = new ByteArrayOutputStream();
try (final var os = new BufferedOutputStream(out)) {
final byte[] b = {10, 20, 30, 40, 50};
os.write(b, 0, 2);
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
os.write(b, 2, 3);
os.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 40, 50]
}
void write (int b)
final var out = new ByteArrayOutputStream();
try (final var os = new BufferedOutputStream(out)) {
os.write(10);
os.write(20);
os.write(30);
}
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
Methods declared in FilterOutputStream
Methods declared in OutputStream
Related posts
- Ejemplos de API