public class TeeWriter extends Writer
tee command. This class
enables writing the contents of an InputStream to
multiple Writers.
The following code shows sample usage of this class.
import com.sptci.io.TeeWriter;
import java.io.BufferedReader;
import java.io.FileReader;
public class test
{
public static void main( String[] args )
{
try
{
String[] files = {"/tmp/one", "/tmp/two", "/tmp/three"};;
TeeWriter writer = new TeeWriter( files );
BufferedReader reader = new BufferedReader(
new FileReader( "/tmp/test.java" ) );
String line = "";
while ( ( line = reader.readLine() ) != null )
{
writer.write( line );
writer.write( System.getProperty( "line.separator" ) );
}
reader.close();
writer.flush();
writer.close();
}
catch ( Throwable t )
{
t.printStackTrace();
}
}
}
FileUtilities.writeToFiles(java.io.InputStream, String[]),
© Copyright 2005, Sans Pareil Technologies, Inc.
| Constructor and Description |
|---|
TeeWriter(File[] files)
Create a new instance that writes all the data to the specified
files.
|
TeeWriter(String[] names)
Create a new instance that writes all the data to the specified
files.
|
TeeWriter(Writer[] writers)
Create a new instance that writes all the data to the specified
writers.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addWriter(Writer writer)
|
void |
close()
Close the stream, flushing it first.
|
void |
flush()
Flush the stream.
|
Writer[] |
getWriters()
Returns
writers. |
void |
setWriters(Writer[] writers)
Set
writers. |
void |
write(char[] chars)
Writes
chars.length characters from the specified
char array to all the output streams. |
void |
write(char[] chars,
int offset,
int length)
Writes
length characters from the specified char
array starting at offset offset to the writers. |
void |
write(int character)
Write a single character.
|
void |
write(String string)
Write the string to the
writers. |
void |
write(String string,
int offset,
int length)
Write the portion of the string to the
writers. |
public TeeWriter(Writer[] writers)
writers - The array of
Writers to which the contents are to be
written.public TeeWriter(String[] names) throws IOException
flush()
method if this constructor is used.
Note: If the file(s) specified point to paths that do not yet exist, the required directory structure will be created.
names - The array of fully qualified filenames to which
the data is to be written.IOException - If errors are encountered while creating the
specified files.createWriters( String[] )public TeeWriter(File[] files) throws IOException
flush()
method if this constructor is used.
Note: If the file(s) specified point to paths that do not yet exist, the required directory structure will be created.
files - The array of files to which the data is to be
written.IOException - If errors are encountered while creating the
specified files.createWriters( File[] )public final void addWriter(Writer writer)
writer - The writer to add.public void close()
throws IOException
write(int) or flush() invocations
will cause an IOException to be thrown. Closing a
previously-closed stream, however, has no effect.
Iterates through the writers and closes each of them.close in interface Closeableclose in interface AutoCloseableclose in class WriterIOException - If errors are encountered while closing any
of the configured output streams.public void flush()
throws IOException
write(int) methods in a buffer, write them
immediately to their intended destination. Then, if that
destination is another character or byte stream, flush it. Thus
one flush() invocation will flush all the buffers in
a chain of Writers and OutputStreams. Iteratores through the
writers and flushes each sequentially.flush in interface Flushableflush in class WriterIOException - If errors are encountered while flushing any
of the configured output streams.public void write(int character)
throws IOException
write in class Writercharacter - The character to be written.IOException - If errors are encountered while writing the
byte to any of the configured output streams.public void write(char[] chars)
throws IOException
chars.length characters from the specified
char array to all the output streams.write in class Writerchars - The data that is to be written.IOException - If errors are encountered while writing the
data to any of the configured output streams.public void write(char[] chars,
int offset,
int length)
throws IOException,
NullPointerException,
IndexOutOfBoundsException
length characters from the specified char
array starting at offset offset to the writers.write in class Writerchars - The data to be written.offset - The start offset in the array.length - The number of characters to write.IOException - If an I/O error occurs. In particular, an
IOException is thrown if the writer is closed.NullPointerExceptionIndexOutOfBoundsExceptionpublic void write(String string) throws IOException
writers.write in class Writerstring - The string to be written.IOException - If an I/O error occurs. In particular, an
IOException is thrown if the writer is closed.public void write(String string, int offset, int length) throws IOException
writers.write in class Writerstring - The string to be written.offset - The start offset in the string.length - The number of characters to write.IOException - If an I/O error occurs. In particular, an
IOException is thrown if the writer is closed.public final Writer[] getWriters()
writers.