Apache Commons IO - TeeOutputStream
TeeOutputStream divise le OutputStream. Il est nommé d'après la commande unix «tee». Il permet à un flux d'être ramifié en deux flux.
Déclaration de classe
Voici la déclaration pour org.apache.commons.io.output.TeeOutputStream Classe -
public class TeeOutputStream
extends ProxyOutputStream
Exemple de classe TeeOutputStream
Dans cet exemple, TeeOutputStream accepte deux flux de sortie en tant que paramètre et transmet les données aux données d'ensemble TeeOutputStream aux deux flux de sortie.
IOTester.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.TeeOutputStream;
public class IOTester {
private static final String SAMPLE = "Welcome to TutorialsPoint. Simply Easy
Learning.";
public static void main(String[] args) {
try {
usingTeeInputStream();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
public static void usingTeeInputStream() throws IOException {
TeeInputStream teeInputStream = null;
TeeOutputStream teeOutputStream = null;
try {
ByteArrayInputStream inputStream = new
ByteArrayInputStream(SAMPLE.getBytes("US-ASCII"));
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);
teeInputStream = new TeeInputStream(inputStream, teeOutputStream, true);
teeInputStream.read(new byte[SAMPLE.length()]);
System.out.println("Output stream 1: " + outputStream1.toString());
System.out.println("Output stream 2: " + outputStream2.toString());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
//teeIn.close() closes teeIn and teeOut which in turn closes the out1 and out2.
try {
teeInputStream.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
Production
Il imprimera le résultat suivant.
Output stream 1: Welcome to TutorialsPoint. Simply Easy Learning.
Output stream 2: Welcome to TutorialsPoint. Simply Easy Learning.