Apache Commons IO Tutorial
Selected Reading
- Apache Commons IO - Discussion
- Apache Commons IO - Useful Resources
- Apache Commons IO - Quick Guide
- Apache Commons IO - TeeOutputStream
- Apache Commons IO - TeeInputStream
- LastModifiedFileComparator
- Apache Commons IO - SizeFileComparator
- Apache Commons IO - NameFileComparator
- Apache Commons IO - FileAlterationMonitor
- Apache Commons IO - FileAlterationObserver
- Apache Commons IO - FileEntry
- Apache Commons IO - AndFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - NameFileFilter
- Apache Commons IO - LineIterator
- Apache Commons IO - IOCase
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - IOUtils
- Apache Commons IO - Environment Setup
- Apache Commons IO - Overview
- Apache Commons IO - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache Commons IO - TeeOutputStream
Apache Commons IO - TeeOutputStream
TeeOutputStream sppts the OutputStream. It is named after the unix tee command. It allows a stream to be branched to two streams.
Class Declaration
Following is the declaration for org.apache.commons.io.output.TeeOutputStream Class −
pubpc class TeeOutputStream extends ProxyOutputStream
Example of TeeOutputStream Class
In this example, TeeOutputStream accepts two output streams as parameter and passing data to TeeOutputStream set data to both output streams.
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; pubpc class IOTester { private static final String SAMPLE = "Welcome to TutorialsPoint. Simply Easy Learning."; pubpc static void main(String[] args) { try { usingTeeInputStream(); } catch(IOException e) { System.out.println(e.getMessage()); } } pubpc 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
It will print the following result.
Output stream 1: Welcome to TutorialsPoint. Simply Easy Learning. Output stream 2: Welcome to TutorialsPoint. Simply Easy Learning.Advertisements