Class Tee
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
,RevisionHandler
E.g., for redirecting stderr/stdout to files with timestamps and:
import java.io.*; import weka.core.Tee; ... // stdout Tee teeOut = new Tee(System.out); teeOut.add(new PrintStream(new FileOutputStream("out.txt")), true); System.setOut(teeOut); // stderr Tee teeErr = new Tee(System.err); teeErr.add(new PrintStream(new FileOutputStream("err.txt")), true); System.setOut(teeErr); ...
- Version:
- $Revision: 15286 $
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
-
Constructor Summary
ConstructorDescriptionTee()
initializes the object, with a default printstream.Tee
(PrintStream def) initializes the object with the given default printstream, e.g., System.out. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(PrintStream p) adds the given PrintStream to the list of streams, with NO timestamp and NO prefix.void
add
(PrintStream p, boolean timestamp) adds the given PrintStream to the list of streams, with NO prefix.void
add
(PrintStream p, boolean timestamp, String prefix) adds the given PrintStream to the list of streams.append
(char c) Appends the specified character to this output stream.append
(CharSequence csq) Appends the specified character sequence to this output stream.append
(CharSequence csq, int start, int end) Appends a subsequence of the specified character sequence to this output stream.void
clear()
removes all streams and places the default printstream, if any, again in the list.boolean
checks whether the given PrintStream is already in the list.void
flush()
flushes all the printstreams.get
(int index) returns the specified PrintStream from the list.returns the default printstrean, can be NULL.Returns the revision string.void
print
(boolean x) prints the given boolean to the streams.void
print
(char x) prints the given char to the streams.void
print
(char[] x) prints the given char array to the streams.void
print
(double x) prints the given double to the streams.void
print
(float x) prints the given float to the streams.void
print
(int x) prints the given int to the streams.void
print
(long x) prints the given long to the streams.void
prints the given object to the streams.void
prints the given string to the streams.A convenience method to write a formatted string to this output stream using the specified format string and arguments.A convenience method to write a formatted string to this output stream using the specified format string and arguments.void
println()
prints a new line to the streams.void
println
(boolean x) prints the given boolean to the streams.void
println
(char x) prints the given char to the streams.void
println
(char[] x) prints the given char array to the streams.void
println
(double x) prints the given double to the streams.void
println
(float x) prints the given float to the streams.void
println
(int x) prints the given int to the streams.void
println
(long x) prints the given long to the streams.void
prints the given object to the streams (for Throwables we print the stack trace).void
prints the given string to the streams.remove
(int index) removes the given PrintStream from the list.removes the given PrintStream from the list.int
size()
returns the number of streams currently in the list.toString()
returns only the classname and the number of streams.void
write
(byte[] b) Writesb.length
bytes to this output stream.void
write
(byte[] buf, int off, int len) Writeslen
bytes from the specified byte array starting at offsetoff
to this stream.void
write
(int b) Writes the specified byte to this stream.Methods inherited from class java.io.PrintStream
checkError, close, format, format, writeBytes
Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
Tee
public Tee()initializes the object, with a default printstream. -
Tee
initializes the object with the given default printstream, e.g., System.out.- Parameters:
def
- the default printstream, remains also after calling clear()
-
-
Method Details
-
clear
public void clear()removes all streams and places the default printstream, if any, again in the list.- See Also:
-
getDefault
returns the default printstrean, can be NULL.- Returns:
- the default printstream
- See Also:
-
m_Default
-
add
adds the given PrintStream to the list of streams, with NO timestamp and NO prefix.- Parameters:
p
- the printstream to add
-
add
adds the given PrintStream to the list of streams, with NO prefix.- Parameters:
p
- the printstream to addtimestamp
- whether to use timestamps or not
-
add
adds the given PrintStream to the list of streams.- Parameters:
p
- the printstream to addtimestamp
- whether to use timestamps or notprefix
- the prefix to use
-
get
returns the specified PrintStream from the list.- Parameters:
index
- the index of the PrintStream to return- Returns:
- the specified PrintStream, or null if invalid index
-
remove
removes the given PrintStream from the list.- Parameters:
p
- the PrintStream to remove- Returns:
- returns the removed PrintStream if it could be removed, null otherwise
-
remove
removes the given PrintStream from the list.- Parameters:
index
- the index of the PrintStream to remove- Returns:
- returns the removed PrintStream if it could be removed, null otherwise
-
contains
checks whether the given PrintStream is already in the list.- Parameters:
p
- the PrintStream to look for- Returns:
- true if the PrintStream is in the list
-
size
public int size()returns the number of streams currently in the list.- Returns:
- the number of streams in the list
-
flush
public void flush()flushes all the printstreams.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classPrintStream
-
print
public void print(int x) prints the given int to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
print
public void print(long x) prints the given long to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
print
public void print(float x) prints the given float to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
print
public void print(double x) prints the given double to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
print
public void print(boolean x) prints the given boolean to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
print
public void print(char x) prints the given char to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
print
public void print(char[] x) prints the given char array to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
print
prints the given string to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
print
prints the given object to the streams.- Overrides:
print
in classPrintStream
- Parameters:
x
- the object to print
-
println
public void println()prints a new line to the streams.- Overrides:
println
in classPrintStream
-
println
public void println(int x) prints the given int to the streams.- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
println
public void println(long x) prints the given long to the streams.- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
println
public void println(float x) prints the given float to the streams.- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
println
public void println(double x) prints the given double to the streams.- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
println
public void println(boolean x) prints the given boolean to the streams.- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
println
public void println(char x) prints the given char to the streams.- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
println
public void println(char[] x) prints the given char array to the streams.- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
println
prints the given string to the streams.- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
println
prints the given object to the streams (for Throwables we print the stack trace).- Overrides:
println
in classPrintStream
- Parameters:
x
- the object to print
-
write
public void write(byte[] buf, int off, int len) Writeslen
bytes from the specified byte array starting at offsetoff
to this stream. If automatic flushing is enabled then theflush
method will be invoked.Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the
print(char)
orprintln(char)
methods.- Overrides:
write
in classPrintStream
- Parameters:
buf
- A byte arrayoff
- Offset from which to start taking byteslen
- Number of bytes to write
-
write
public void write(int b) Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then theflush
method will be invoked.Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the
print(char)
orprintln(char)
methods.- Overrides:
write
in classPrintStream
- Parameters:
b
- The byte to be written- See Also:
-
write
Writesb.length
bytes to this output stream.The
write
method ofFilterOutputStream
calls itswrite
method of three arguments with the argumentsb
,0
, andb.length
.Note that this method does not call the one-argument
write
method of its underlying output stream with the single argumentb
.- Overrides:
write
in classPrintStream
- Parameters:
b
- the data to be written.- Throws:
IOException
- if an I/O error occurs.- See Also:
-
printf
A convenience method to write a formatted string to this output stream using the specified format string and arguments.An invocation of this method of the form
out.printf(format, args)
behaves in exactly the same way as the invocationout.format(format, args)
- Overrides:
printf
in classPrintStream
- Parameters:
format
- A format string as described in Format string syntaxargs
- Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on anull
argument depends on the conversion.- Returns:
- This output stream
- Throws:
IllegalFormatException
- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.NullPointerException
- If theformat
isnull
- Since:
- 1.5
-
printf
A convenience method to write a formatted string to this output stream using the specified format string and arguments.An invocation of this method of the form
out.printf(l, format, args)
behaves in exactly the same way as the invocationout.format(l, format, args)
- Overrides:
printf
in classPrintStream
- Parameters:
l
- The locale to apply during formatting. Ifl
isnull
then no localization is applied.format
- A format string as described in Format string syntaxargs
- Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on anull
argument depends on the conversion.- Returns:
- This output stream
- Throws:
IllegalFormatException
- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.NullPointerException
- If theformat
isnull
- Since:
- 1.5
-
append
Appends the specified character to this output stream.An invocation of this method of the form
out.append(c)
behaves in exactly the same way as the invocationout.print(c)
- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classPrintStream
- Parameters:
c
- The 16-bit character to append- Returns:
- This output stream
- Since:
- 1.5
-
append
Appends the specified character sequence to this output stream.An invocation of this method of the form
out.append(csq)
behaves in exactly the same way as the invocationout.print(csq.toString())
Depending on the specification of
toString
for the character sequencecsq
, the entire sequence may not be appended. For instance, invoking thentoString
method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classPrintStream
- Parameters:
csq
- The character sequence to append. Ifcsq
isnull
, then the four characters"null"
are appended to this output stream.- Returns:
- This output stream
- Since:
- 1.5
-
append
Appends a subsequence of the specified character sequence to this output stream.An invocation of this method of the form
out.append(csq, start, end)
whencsq
is notnull
, behaves in exactly the same way as the invocationout.print(csq.subSequence(start, end).toString())
- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classPrintStream
- Parameters:
csq
- The character sequence from which a subsequence will be appended. Ifcsq
isnull
, then characters will be appended as ifcsq
contained the four characters"null"
.start
- The index of the first character in the subsequenceend
- The index of the character following the last character in the subsequence- Returns:
- This output stream
- Throws:
IndexOutOfBoundsException
- Ifstart
orend
are negative,start
is greater thanend
, orend
is greater thancsq.length()
- Since:
- 1.5
-
toString
returns only the classname and the number of streams. -
getRevision
Returns the revision string.- Specified by:
getRevision
in interfaceRevisionHandler
- Returns:
- the revision
-