Java : OutputStream con ejemplos
OutputStream (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de OutputStream.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Esta clase abstracta es la superclase de todas las clases que representan un flujo de salida de bytes. Un flujo de salida acepta bytes de salida y los envía a algún receptor. (Traducción automática)
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
try (final var os = Files.newOutputStream(file)) {
os.write(10);
os.write(20);
os.write(30);
}
final var bytes = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes)); // [10, 20, 30]
Constructors
OutputStream ()
Constructor para que las subclases llamen. (Traducción automática)
I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Methods
void close ()
Cierra este flujo de salida y libera todos los recursos del sistema asociados con este flujo. (Traducción automática)
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
try (final var os = Files.newOutputStream(file)) {
os.write(123);
}
final var bytes = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes)); // [123]
// An example without a try-with-resources statement.
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
final var os = Files.newOutputStream(file);
try {
os.write(123);
} finally {
os.close();
}
final var bytes = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes)); // [123]
void flush ()
Limpia este flujo de salida y fuerza a que se escriban todos los bytes de salida almacenados en búfer. (Traducción automática)
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(file))) {
os.write(123);
System.out.println(Arrays.toString(Files.readAllBytes(file))); // []
os.flush();
System.out.println(Arrays.toString(Files.readAllBytes(file))); // [123]
}
static OutputStream nullOutputStream ()
Devuelve un nuevo OutputStream que descarta todos los bytes. (Traducción automática)
final byte[] b = {1, 2, 3};
try (final var os = OutputStream.nullOutputStream()) {
// A stream discards all bytes.
os.write(123);
os.write(b);
os.write(b, 0, 2);
os.flush();
}
void write (byte[] b)
Escribe bytes de longitud b.length de la matriz de bytes especificada en este flujo de salida. (Traducción automática)
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
try (final var os = Files.newOutputStream(file)) {
os.write(new byte[]{10, 20});
final var bytes1 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes1)); // [10, 20]
os.write(new byte[]{30, 40, 50});
final var bytes2 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes2)); // [10, 20, 30, 40, 50]
}
void write (byte[] b, int off, int len)
Escribe len bytes de la matriz de bytes especificada comenzando en el desplazamiento off en esta secuencia de salida. (Traducción automática)
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
final byte[] b = {10, 20, 30, 40, 50};
try (final var os = Files.newOutputStream(file)) {
os.write(b, 0, 2);
final var bytes1 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes1)); // [10, 20]
os.write(b, 2, 3);
final var bytes2 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes2)); // [10, 20, 30, 40, 50]
}
abstract void write (int b)
Escribe el byte especificado en este flujo de salida. (Traducción automática)
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data
try (final var os = Files.newOutputStream(file)) {
os.write(1);
final var bytes1 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes1)); // [1]
os.write(127);
final var bytes2 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes2)); // [1, 127]
os.write(128);
final var bytes3 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes3)); // [1, 127, -128]
os.write(129);
final var bytes4 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes4)); // [1, 127, -128, -127]
os.write(255);
final var bytes5 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes5)); // [1, 127, -128, -127, -1]
os.write(256);
final var bytes6 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes6)); // [1, 127, -128, -127, -1, 0]
os.write(257);
final var bytes7 = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes7)); // [1, 127, -128, -127, -1, 0, 1]
}
Related posts
- Ejemplos de API
- Closeable
- DataInput
- DataOutput
- Flushable
- InputStream
- IOException
- OutputStream
- Reader
- Writer