📜  Java Java类设置 1

📅  最后修改于: 2022-05-13 01:55:37.444000             🧑  作者: Mango

Java Java类设置 1

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(File file):使用指定的文件创建一个新的打印流,不自动刷新行。
  • PrintStream(File file, 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) :将指定的字符附加到此输出流。
    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):将指定的字符序列附加到此输出流。
    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) :将指定字符序列的子序列附加到此输出流。
    Syntax :public PrintStream append(CharSequence csq)
    Parameters:
    csq - The character sequence to append. 
    Returns:
    This output stream
    
  • boolean checkError():刷新流并检查其错误状态。
    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() :清除此流的内部错误状态。
    Syntax :protected void clearError()
    
  • void close() :关闭流。
    Syntax :public void close()
    Overrides:
    close in class FilterOutputStream
  • void flush():刷新流。
    Syntax :public void flush()
    Overrides:
    flush in class FilterOutputStream
  • PrintStream format(Locale l, String format, Object... args):使用指定的格式字符串和参数将格式化字符串写入此输出流。
    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 format(String format, Object... args):使用指定的格式字符串和参数将格式化字符串写入此输出流。
    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):打印一个布尔值。
    Syntax :public void print(boolean b)
  • void print(char c):打印一个字符。
    Syntax :public void print(char c)
  • void print(char[] s):打印一个字符数组。
    Syntax :public void print(char[] s)
    
  • void print(double d) :打印一个双精度浮点数。
    Syntax :public void print(double b)
    
  • void print(float f):打印一个浮点数。
    Syntax :public void print(float f)
  • void print(int i):打印一个整数。
    Syntax :public void print(int i)
  • void print(long l):打印一个长整数。
    Syntax :public void print(long l)
  • void print(Object obj) :打印一个对象。
    Syntax :public void print(Object obj)
  • void print(String s):打印一个字符串。
    Syntax :public void print(String s)
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();
    }
}

注意:在线 IDE 上可能看不到输出,因为它无法读取文件。
输出:

true14.533GeeksforGeeks
java.io.FileOutputStream@1540e19dGeek
false
Welcome to my First program

下一篇: Java.io.Printstream 类Java |设置 2