logo

Clasa Java.io.Printstream în Java | Setul 1

Un PrintStream adaugă funcționalitate unui alt flux de ieșire și anume capacitatea de a imprima convenabil reprezentări ale diferitelor valori de date. Spre deosebire de alte fluxuri de ieșire, un PrintStream nu aruncă niciodată o IOException; în schimb, situațiile excepționale stabilesc doar un semnal intern care poate fi testat prin metoda checkError. Opțional, un PrintStream poate fi creat pentru a se spăla automat. Toate caracterele imprimate de un PrintStream sunt convertite în octeți utilizând codificarea implicită a caracterelor a platformei. Clasa PrintWriter ar trebui utilizată în situațiile care necesită scrierea de caractere, mai degrabă decât de octeți. Declarație de clasă
public class PrintStream extends FilterOutputStream implements Appendable Closeable
Domeniu
 protected OutputStream out:This is the output stream to be filtered. 
Constructori și descriere
    PrintStream(Fișier fișier):Creează un nou flux de imprimare fără spălarea automată a liniei cu fișierul specificat. PrintStream(Fișier fișier String csn):Creează un nou flux de imprimare fără spălarea automată a liniei cu fișierul și setul de caractere specificate. PrintStream(outputStream out):Creează un nou flux de imprimare. PrintStream(OutputStream out boolean autoFlush):Creează un nou flux de imprimare. PrintStream(OutputStream out boolean autoFlush String codificare): creează un nou flux de imprimare. PrintStream(String fileName):Creează un nou flux de imprimare fără spălarea automată a liniei cu numele fișierului specificat. PrintStream(String fileName String csn):Creează un nou flux de imprimare fără spălarea automată a liniei cu numele și setul de caractere specificate.
Metode:
    PrintStream append(car 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 
    boolean 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
    protected void 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
    Format PrintStream (Locale l Format String 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
    Format PrintStream(Format șir obiect... 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)
    void print(car c): Prints a character.
      Syntax :  public void print(char c)
    void print(car[] s): Prints an array of characters.
      Syntax :  public void print(char[] s) 
    imprimare nulă (d dublu): Prints a double-precision floating-point number.
      Syntax :  public void print(double b) 
    imprimare nulă (float f): Prints a floating-point number.
      Syntax :  public void print(float f)
    void print(int i): Prints an integer.
      Syntax :  public void print(int i)
    imprimare goală (l lung): Prints a long integer.
      Syntax :  public void print(long l)
    void print(Obiect obiect) : Prints an object.
      Syntax :  public void print(Object obj)
    imprimare nulă (șir de caractere): Prints a string.
      Syntax :  public void print(String s)
Java
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Locale; //Java program to demonstrate PrintStream methods class Printstream {  public static void main(String args[]) throws FileNotFoundException  {  FileOutputStream fout=new FileOutputStream('file.txt');    //creating Printstream obj  PrintStream out=new PrintStream(fout);  String s='First';  //writing to file.txt  char c[]={'G''E''E''K'};    //illustrating print(boolean b) method  out.print(true);    //illustrating print(int i) method  out.print(1);    //illustrating print(float f) method  out.print(4.533f);    //illustrating print(String s) method  out.print('GeeksforGeeks');  out.println();    //illustrating print(Object Obj) method  out.print(fout);  out.println();    //illustrating append(CharSequence csq) method  out.append('Geek');  out.println();    //illustrating checkError() method  out.println(out.checkError());    //illustrating format() method  out.format(Locale.UK 'Welcome to my %s program' s);    //illustrating flush method  out.flush();    //illustrating close method  out.close();  } } 
Note: The output might not be visible on online IDE as it is not able to read the file. Ieșire:
true14.533GeeksforGeeks java.io.FileOutputStream@1540e19dGeek false Welcome to my First program
Articolul următor: Clasa Java.io.Printstream în Java | Setul 2 Creați un test