PrintStream добавя функционалност към друг изходен поток, а именно възможността за удобно отпечатване на представяния на различни стойности на данни. За разлика от други изходни потоци PrintStream никога не хвърля IOException; вместо това изключителните ситуации просто задават вътрешен флаг, който може да бъде тестван чрез метода checkError. По избор може да се създаде PrintStream, така че да се промива автоматично. Всички знаци, отпечатани от PrintStream, се преобразуват в байтове, като се използва стандартното кодиране на знаци на платформата. Класът PrintWriter трябва да се използва в ситуации, които изискват писане на символи, а не на байтове. Декларация на клас
public class PrintStream extends FilterOutputStream implements Appendable Closeable
Поле
protected OutputStream out:This is the output stream to be filtered.
Конструктори и описание
PrintStream (Файлов файл):
Създава нов поток за печат без автоматично изчистване на реда с посочения файл.
PrintStream(Файлов файл String csn):
Създава нов поток за печат без автоматично изчистване на редове с посочения файл и набор от знаци.
PrintStream(OutputStream out) :
Създава нов поток за печат.
PrintStream(OutputStream out boolean autoFlush) :
Създава нов поток за печат.
PrintStream(OutputStream out boolean autoFlush String encoding)
: Създава нов поток за печат.
PrintStream(String fileName):
Създава нов поток за печат без автоматично изчистване на ред с указаното име на файл.
PrintStream(String fileName String csn):
Създава нов поток за печат без автоматично изчистване на ред с указаното име на файл и набор от знаци. Методи:
PrintStream append(char 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
PrintStream append(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
PrintStream append(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
булево checkError():
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() :
Clears the internal error state of this stream.
Syntax : protected void clearError()
void close():
Closes the stream.
Syntax : public void close() Overrides: close in class FilterOutputStream
void flush():
Flushes the stream.
Syntax : public void flush() Overrides: flush in class FilterOutputStream
Формат на PrintStream (Locale l String format Object... args):
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
PrintStream формат (String format Object... args):
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
void print(boolean b):
Prints a boolean value.
Syntax : public void print(boolean b)
празен печат (char c):
Prints a character.
Syntax : public void print(char c)
void print(char[] s):
Prints an array of characters.
Syntax : public void print(char[] s)
празен печат (двойно d) :
Prints a double-precision floating-point number.
Syntax : public void print(double b)
void print(float f):
Prints a floating-point number.
Syntax : public void print(float f)
празен печат (int i):
Prints an integer.
Syntax : public void print(int i)
празен печат (дълъг l):
Prints a long integer.
Syntax : public void print(long l)
void print(Object obj):
Prints an object.
Syntax : public void print(Object obj)
празен печат (низ 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. Изход:
true14.533GeeksforGeeks java.io.FileOutputStream@1540e19dGeek false Welcome to my First program
Следваща статия: Java.io.Printstream клас в Java | Комплект 2 Създаване на тест