logo

Java.io.Printstream klase Java | 1. komplekts

PrintStream pievieno funkcionalitāti citai izvades straumei, proti, iespēju ērti drukāt dažādu datu vērtību attēlojumus. Atšķirībā no citām izvades straumēm PrintStream nekad neizraisa IOException; tā vietā ārkārtējās situācijās vienkārši tiek iestatīts iekšējais karodziņš, ko var pārbaudīt, izmantojot checkError metodi. Pēc izvēles var izveidot PrintStream, lai tas tiktu automātiski izskalots. Visas PrintStream izdrukātās rakstzīmes tiek pārveidotas baitos, izmantojot platformas noklusējuma rakstzīmju kodējumu. PrintWriter klase ir jāizmanto situācijās, kurās ir jāraksta rakstzīmes, nevis baiti. Klases deklarācija
public class PrintStream extends FilterOutputStream implements Appendable Closeable
Lauks
 protected OutputStream out:This is the output stream to be filtered. 
Konstruktori un apraksts
    PrintStream (faila fails):Izveido jaunu drukas straumi bez automātiskas līnijas skalošanas ar norādīto failu. PrintStream (faila faila virkne csn):Izveido jaunu drukas straumi bez automātiskas līnijas skalošanas ar norādīto failu un rakstzīmju kopu. PrintStream (OutputStream out):Izveido jaunu drukas straumi. PrintStream (OutputStream out Būla automātiskā skalošana):Izveido jaunu drukas straumi. PrintStream (OutputStream out Būla autoFlush String kodējums): izveido jaunu drukas straumi. PrintStream (virknes faila nosaukums) :Izveido jaunu drukas straumi bez automātiskas līnijas skalošanas ar norādīto faila nosaukumu. PrintStream (virknes faila nosaukums String csn) :Izveido jaunu drukas straumi bez automātiskas rindas skalošanas ar norādīto faila nosaukumu un rakstzīmju kopu.
Metodes:
    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 sākums int beigas): 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 pievienošana(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 
    Būla pārbaudes kļūda(): 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() 
    tukša aizvēršana () : 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 formāts (Locale l String formāts 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 formāts (virknes formāts 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 
    tukša druka (būla b): Prints a boolean value.
      Syntax :  public void print(boolean b)
    tukša izdruka (char c): Prints a character.
      Syntax :  public void print(char c)
    tukša druka(char[] s): Prints an array of characters.
      Syntax :  public void print(char[] s) 
    tukša druka (dubultā d): Prints a double-precision floating-point number.
      Syntax :  public void print(double b) 
    tukša druka (peldošs f): Prints a floating-point number.
      Syntax :  public void print(float f)
    tukša izdruka (int i): Prints an integer.
      Syntax :  public void print(int i)
    tukša druka (garš l): Prints a long integer.
      Syntax :  public void print(long l)
    tukša druka (Objekta objekts): Prints an object.
      Syntax :  public void print(Object obj)
    tukša druka (virknes): 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. Izvade:
true14.533GeeksforGeeks java.io.FileOutputStream@1540e19dGeek false Welcome to my First program
Nākamais raksts: Java.io.Printstream klase Java | 2. komplekts Izveidojiet viktorīnu