Java : PipedWriter con ejemplos
PipedWriter (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de PipedWriter.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
try (final var reader = new PipedReader();
final var writer = new PipedWriter(reader)) {
try (final var executor = Executors.newFixedThreadPool(2)) {
executor.submit(() -> {
try (writer) {
writer.write("abc");
TimeUnit.SECONDS.sleep(1);
writer.write('X');
TimeUnit.SECONDS.sleep(1);
writer.write('Y');
TimeUnit.SECONDS.sleep(1);
writer.write('Z');
TimeUnit.SECONDS.sleep(1);
} catch (IOException e) {
System.out.println("Writer IOException! : " + e.getMessage());
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
executor.submit(() -> {
try (reader) {
System.out.println("-- start --");
while (true) {
final var c = reader.read();
if (c == -1) {
System.out.println("-- end --");
break;
}
System.out.printf("%c%n", c);
}
} catch (IOException e) {
System.out.println("Reader IOException! : " + e.getMessage());
}
});
}
}
// Result
// ↓
//-- start --
//a
//b
//c
//X
//Y
//Z
//-- end --
Fields declared in Writer
Constructors
PipedWriter ()
try (final var reader = new PipedReader();
final var writer = new PipedWriter()) {
writer.connect(reader);
try (final var executor = Executors.newFixedThreadPool(2)) {
executor.submit(() -> {
try (writer) {
final var list = List.of('a', 'b', 'c', 'd');
for (final var c : list) {
writer.write(c);
TimeUnit.SECONDS.sleep(1);
}
} catch (IOException e) {
System.out.println("Writer IOException! : " + e.getMessage());
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
executor.submit(() -> {
try (reader) {
System.out.println("-- start --");
while (true) {
final var c = reader.read();
if (c == -1) {
System.out.println("-- end --");
break;
}
System.out.printf("%c%n", c);
}
} catch (IOException e) {
System.out.println("Reader IOException! : " + e.getMessage());
}
});
}
}
// Result
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
PipedWriter (PipedReader snk)
try (final var reader = new PipedReader();
final var writer = new PipedWriter(reader)) {
try (final var executor = Executors.newFixedThreadPool(2)) {
executor.submit(() -> {
try (writer) {
final var list = List.of('a', 'b', 'c', 'd');
for (final var c : list) {
writer.write(c);
TimeUnit.SECONDS.sleep(1);
}
} catch (IOException e) {
System.out.println("Writer IOException! : " + e.getMessage());
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
executor.submit(() -> {
try (reader) {
System.out.println("-- start --");
while (true) {
final var c = reader.read();
if (c == -1) {
System.out.println("-- end --");
break;
}
System.out.printf("%c%n", c);
}
} catch (IOException e) {
System.out.println("Reader IOException! : " + e.getMessage());
}
});
}
}
// Result
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
Methods
void close ()
try (final var reader = new PipedReader();
final var writer = new PipedWriter(reader)) {
writer.write('a');
System.out.printf("%c%n", reader.read()); // a
}
// An example without a try-with-resources statement.
final var writer = new PipedWriter();
try (final var reader = new PipedReader(writer)) {
writer.write('a');
System.out.printf("%c%n", reader.read()); // a
} finally {
writer.close();
}
void connect (PipedReader snk)
try (final var reader = new PipedReader();
final var writer = new PipedWriter()) {
writer.connect(reader);
try (final var executor = Executors.newFixedThreadPool(2)) {
executor.submit(() -> {
try (writer) {
final var list = List.of('a', 'b', 'c', 'd');
for (final var c : list) {
writer.write(c);
TimeUnit.SECONDS.sleep(1);
}
} catch (IOException e) {
System.out.println("Writer IOException! : " + e.getMessage());
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
executor.submit(() -> {
try (reader) {
System.out.println("-- start --");
while (true) {
final var c = reader.read();
if (c == -1) {
System.out.println("-- end --");
break;
}
System.out.printf("%c%n", c);
}
} catch (IOException e) {
System.out.println("Reader IOException! : " + e.getMessage());
}
});
}
}
// Result
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
void flush ()
// Gets the elapsed seconds from the start time.
final long startTime = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - startTime) / 1000000000.0;
try (final var reader = new PipedReader();
final var writer = new PipedWriter(reader)) {
try (final var executor = Executors.newFixedThreadPool(2)) {
executor.submit(() -> {
try (writer) {
final var list = List.of('a', 'b', 'c', 'd');
for (final var c : list) {
writer.write(c);
writer.flush();
TimeUnit.SECONDS.sleep(1);
}
} catch (IOException e) {
System.out.println("Writer IOException! : " + e.getMessage());
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
executor.submit(() -> {
try (reader) {
System.out.println("-- start --");
while (true) {
final var c = reader.read();
if (c == -1) {
System.out.println("-- end --");
break;
}
System.out.printf("%c (%f sec.)%n", c, elapsedTime.getAsDouble());
}
} catch (IOException e) {
System.out.println("Reader IOException! : " + e.getMessage());
}
});
}
}
// Result
// ↓
//-- start --
//a (0.002405 sec.)
//b (1.007973 sec.)
//c (2.018421 sec.)
//d (3.024290 sec.)
//-- end --
void write (char[] cbuf, int off, int len)
try (final var reader = new PipedReader();
final var writer = new PipedWriter(reader)) {
final char[] cbuf = {'a', 'b', 'c'};
{
writer.write(cbuf, 0, 1);
final var a = new char[3];
final var ret = reader.read(a);
// ret = 1 : [a, , ]
System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
}
{
writer.write(cbuf, 0, 2);
final var a = new char[3];
final var ret = reader.read(a);
// ret = 2 : [a, b, ]
System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
}
{
writer.write(cbuf, 0, 3);
final var a = new char[3];
final var ret = reader.read(a);
// ret = 3 : [a, b, c]
System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
}
{
writer.write(cbuf, 1, 2);
final var a = new char[3];
final var ret = reader.read(a);
// ret = 2 : [b, c, ]
System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
}
{
writer.write(cbuf, 2, 1);
final var a = new char[3];
final var ret = reader.read(a);
// ret = 1 : [c, , ]
System.out.printf("ret = %d : %s%n", ret, Arrays.toString(a));
}
}
void write (int c)
try (final var reader = new PipedReader();
final var writer = new PipedWriter(reader)) {
try (final var executor = Executors.newFixedThreadPool(2)) {
executor.submit(() -> {
try (writer) {
final var list = List.of('a', 'b', 'c', 'd');
for (final var c : list) {
writer.write(c);
TimeUnit.SECONDS.sleep(1);
}
} catch (IOException e) {
System.out.println("Writer IOException! : " + e.getMessage());
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
executor.submit(() -> {
try (reader) {
System.out.println("-- start --");
while (true) {
final var c = reader.read();
if (c == -1) {
System.out.println("-- end --");
break;
}
System.out.printf("%c%n", c);
}
} catch (IOException e) {
System.out.println("Reader IOException! : " + e.getMessage());
}
});
}
}
// Result
// ↓
//-- start --
//a
//b
//c
//d
//-- end --
Methods declared in Writer
append, append, append, nullWriter, write, write, write
Consulte el siguiente enlace.
Related posts
- Ejemplos de API