Package Util
Class Out
java.lang.Object
Util.Out
This class serves as a utility for output operations, allowing for redirection
of output streams, file output, custom delimiters, and built-in support for printing
various data types, including arrays and collections.
Output must be flushed with
flush()
by the end of the program
especially when Constants.debug
is true, which is by default
when there an `ONLINE_JUDGE` system property is present (Codeforces, AtCoder).
This is meant to preserve runtime over standard input System.out
by flushing only once.- Since:
- 1.0
- Version:
- 1.0
- Author:
- Sahasrad Chippa
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
flush()
Flushes thePrintWriter
, ensuring all output is written out immediately.static String
Gets the current delimiter used in output methods that handle iterables and arrays.static PrintWriter
Retrieves the current writer object.static void
iterPrint
(boolean[] arr) Iterative printing for boolean arrays, printing each element separated by the current delimiter (a space by default).static void
iterPrint
(char[] arr) Iterative printing for char arrays, printing each element separated by the current delimiter (a space by default).static void
iterPrint
(double[] arr) Iterative printing for double arrays, printing each element separated by the current delimiter (a space by default).static void
iterPrint
(int[] arr) Iterative printing for integer arrays, printing each element separated by the current delimiter (a space by default).static void
iterPrint
(long[] arr) Iterative printing for long arrays, printing each element separated by the current delimiter (a space by default).static <T> void
Prints each element of the given iterable to the output, separated by the current delimiter.static <T> void
iterPrint
(T[] arr) Prints each element of an array to the output, separated by the current delimiter (a space by default).static void
Prints an object to the output.static void
Provides formatted printing usingString.format(java.lang.String, java.lang.Object...)
like syntax.static void
println()
Outputs a new line character to the output stream.static void
Prints an object to the output followed by a newline.static void
setDelimiter
(String delim) Sets a new delimiter for output methods to use when handling iterables and arrays.static void
setOutput
(OutputStream out) Sets the current output stream to a newOutputStream
.static void
setOutputFile
(String fileName) Sets the output to a file with the specified filename.static void
setWriter
(PrintWriter writer) Sets thePrintWriter
to a specified writer, allowing redirection of the output.
-
Constructor Details
-
Out
public Out()
-
-
Method Details
-
getWriter
Retrieves the current writer object.- Returns:
- The current
PrintWriter
instance being used for output.
-
setWriter
Sets thePrintWriter
to a specified writer, allowing redirection of the output.- Parameters:
writer
- The new printer writer.
-
setOutput
Sets the current output stream to a newOutputStream
.- Parameters:
out
- The output stream to write to.
-
setOutputFile
Sets the output to a file with the specified filename.- Parameters:
fileName
- The name of the file to write output to.- Throws:
IllegalArgumentException
- if a file withfileName
cannot be found or created.
-
getDelimiter
Gets the current delimiter used in output methods that handle iterables and arrays.- Returns:
- The current delimiter string.
-
setDelimiter
Sets a new delimiter for output methods to use when handling iterables and arrays.- Parameters:
delim
- The new delimiter string to use.
-
println
public static void println()Outputs a new line character to the output stream. -
print
-
println
-
iterPrint
Prints each element of the given iterable to the output, separated by the current delimiter. Output must be flushed withflush()
by the end of the program- Type Parameters:
T
- The type of elements in the iterable.- Parameters:
arr
- The iterable to print.
-
iterPrint
public static <T> void iterPrint(T[] arr) Prints each element of an array to the output, separated by the current delimiter (a space by default). Output must be flushed withflush()
by the end of the program- Type Parameters:
T
- The type of elements in the array.- Parameters:
arr
- The array to print.
-
iterPrint
public static void iterPrint(int[] arr) Iterative printing for integer arrays, printing each element separated by the current delimiter (a space by default).- Parameters:
arr
- The array to print.
-
iterPrint
public static void iterPrint(long[] arr) Iterative printing for long arrays, printing each element separated by the current delimiter (a space by default).- Parameters:
arr
- The array to print.
-
iterPrint
public static void iterPrint(double[] arr) Iterative printing for double arrays, printing each element separated by the current delimiter (a space by default).- Parameters:
arr
- The array to print.
-
iterPrint
public static void iterPrint(char[] arr) Iterative printing for char arrays, printing each element separated by the current delimiter (a space by default).- Parameters:
arr
- The array to print.
-
iterPrint
public static void iterPrint(boolean[] arr) Iterative printing for boolean arrays, printing each element separated by the current delimiter (a space by default).- Parameters:
arr
- The array to print.
-
printf
Provides formatted printing usingString.format(java.lang.String, java.lang.Object...)
like syntax.- Parameters:
format
- The string format to use.args
- The arguments to use in the format string.
-
flush
public static void flush()Flushes thePrintWriter
, ensuring all output is written out immediately.
-