Un PrintStream agrega funcionalidad a otro flujo de salida, es decir, la capacidad de imprimir cómodamente representaciones de varios valores de datos. A diferencia de otros flujos de salida, PrintStream nunca genera una IOException; en cambio, las situaciones excepcionales simplemente establecen un indicador interno que se puede probar mediante el método checkError. Opcionalmente, se puede crear un PrintStream para que se vacíe automáticamente. Todos los caracteres impresos por PrintStream se convierten en bytes utilizando la codificación de caracteres predeterminada de la plataforma. La clase PrintWriter debe usarse en situaciones que requieren escribir caracteres en lugar de bytes. Declaración de clase
public class PrintStream extends FilterOutputStream implements Appendable Closeable
Campo
protected OutputStream out:This is the output stream to be filtered.
Constructores y descripción
PrintStream (archivo de archivo):
Crea una nueva secuencia de impresión sin vaciado automático de líneas con el archivo especificado.
PrintStream (archivo de archivo String csn):
Crea una nueva secuencia de impresión sin vaciado automático de líneas con el archivo y el juego de caracteres especificados.
PrintStream(OutputStream fuera):
Crea una nueva secuencia de impresión.
PrintStream(OutputStream fuera booleano autoFlush):
Crea una nueva secuencia de impresión.
PrintStream (OutputStream codifica la cadena booleana autoFlush)
: Crea una nueva secuencia de impresión.
PrintStream (nombre de archivo de cadena):
Crea una nueva secuencia de impresión sin vaciado automático de líneas con el nombre de archivo especificado.
PrintStream(String fileName String csn):
Crea una nueva secuencia de impresión sin vaciado automático de líneas con el nombre de archivo y el juego de caracteres especificados. Métodos:
Añadir PrintStream (carácter c):
Appends the specified character to this output stream.
Syntax : public PrintStream append(char c) Parameters: c - The 16-bit character to append Returns: This output stream
Añadir PrintStream (CharSequence csq int start int end):
Appends the specified character sequence to this output stream.
Syntax : public PrintStream append(CharSequence csq int start int end) Parameters: csq - The character sequence from which a subsequence will be appended. start - The index of the first character in the subsequence end - The index of the character following the last character in the subsequence Returns: This output stream Throws: IndexOutOfBoundsException
Añadir PrintStream (CharSequence csq):
Appends a subsequence of the specified character sequence to this output stream.
Syntax : public PrintStream append(CharSequence csq) Parameters: csq - The character sequence to append. Returns: This output stream
error de verificación booleano():
Flushes the stream and checks its error state.
Syntax : public boolean checkError() Returns: true if and only if this stream has encountered an IOException other than InterruptedIOException or the setError method has been invoked
clearError vacío protegido():
Clears the internal error state of this stream.
Syntax : protected void clearError()
cierre vacío() :
Closes the stream.
Syntax : public void close() Overrides: close in class FilterOutputStream
descarga vacía():
Flushes the stream.
Syntax : public void flush() Overrides: flush in class FilterOutputStream
Formato PrintStream (Configuración regional l Formato de cadena Objeto... argumentos):
Writes a formatted string to this output stream using the specified format string and arguments.
Syntax : public PrintStream format(Locale l String format Object... args) Parameters: l - The locale to apply during formatting. If l is null then no localization is applied. format - A format string as described in Format string syntax args - Arguments referenced by the format specifiers in the format string. The number of arguments is variable and may be zero. Returns: This output stream Throws: IllegalFormatException NullPointerException
Formato PrintStream (Objeto en formato de cadena... argumentos):
Writes a formatted string to this output stream using the specified format string and arguments.
Syntax : public PrintStream format(String format Object... args) Parameters : format - A format string as described in Format string syntax args - Arguments referenced by the format specifiers in the format string. The number of arguments is variable and may be zero. Returns: This output stream Throws: IllegalFormatException NullPointerException
impresión nula (booleano b):
Prints a boolean value.
Syntax : public void print(boolean b)
impresión vacía (carácter c):
Prints a character.
Syntax : public void print(char c)
impresión vacía (char[] s):
Prints an array of characters.
Syntax : public void print(char[] s)
impresión nula (doble d):
Prints a double-precision floating-point number.
Syntax : public void print(double b)
impresión vacía (flotante f):
Prints a floating-point number.
Syntax : public void print(float f)
impresión nula (int i):
Prints an integer.
Syntax : public void print(int i)
impresión vacía (largo l):
Prints a long integer.
Syntax : public void print(long l)
impresión vacía (Objeto obj):
Prints an object.
Syntax : public void print(Object obj)
impresión vacía (cadena s):
Prints a string.
Syntax : public void print(String s)
Java
importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.PrintStream;importjava.util.Locale;//Java program to demonstrate PrintStream methodsclassPrintstream{publicstaticvoidmain(Stringargs[])throwsFileNotFoundException{FileOutputStreamfout=newFileOutputStream('file.txt');//creating Printstream objPrintStreamout=newPrintStream(fout);Strings='First';//writing to file.txtcharc[]={'G''E''E''K'};//illustrating print(boolean b) methodout.print(true);//illustrating print(int i) methodout.print(1);//illustrating print(float f) methodout.print(4.533f);//illustrating print(String s) methodout.print('GeeksforGeeks');out.println();//illustrating print(Object Obj) methodout.print(fout);out.println();//illustrating append(CharSequence csq) methodout.append('Geek');out.println();//illustrating checkError() methodout.println(out.checkError());//illustrating format() methodout.format(Locale.UK'Welcome to my %s program's);//illustrating flush methodout.flush();//illustrating close methodout.close();}}
Note: The output might not be visible on online IDE as it is not able to read the file. Producción:
true14.533GeeksforGeeks java.io.FileOutputStream@1540e19dGeek false Welcome to my First program
Artículo siguiente: Clase Java.io.Printstream en Java | Conjunto 2 Crear cuestionario