
- Apache Commons IO 教程
- Apache Commons IO - 主頁
- Apache Commons IO - 概覽
- **Apache Commons IO - 環境設定**
- Apache Commons IO - IOUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - IOCase
- Apache Commons IO - LineIterator
- Apache Commons IO - NameFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - AndFileFilter
- Apache Commons IO - FileEntry
- Apache Commons IO - FileAlterationObserver
- Apache Commons IO - FileAlterationMonitor
- Apache Commons IO - NameFileComparator
- Apache Commons IO - SizeFileComparator
- LastModifiedFileComparator
- Apache Commons IO - TeeInputStream
- **Apache Commons IO - TeeOutputStream**
- Apache Commons IO - 有用資源
- Apache Commons IO - 快速指南
- Apache Commons IO - 有用資源
- Apache Commons IO - 討論
**Apache Commons IO - TeeOutputStream**
TeeOutputStream 分支 OutputStream。它以 unix“tee”命令命名。它允許一個流分支為兩個流。
**類宣告**
以下是 org.apache.commons.io.output.TeeOutputStream 類的宣告:
public class TeeOutputStream extends ProxyOutputStream
**TeeOutputStream 類示例**
在此示例中,TeeOutputStream 接受兩個輸出流作為引數,並將資料傳遞給 TeeOutputStream 來設定資料到兩個輸出流。
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()); } } } }
**輸出**
以下是列印結果:
Output stream 1: Welcome to TutorialsPoint. Simply Easy Learning. Output stream 2: Welcome to TutorialsPoint. Simply Easy Learning.
廣告