JAVA伊奥。Java中的Printstream类|集1

PrintStream为另一个输出流添加了功能,即能够方便地打印各种数据值的表示。与其他输出流不同,PrintStream从不抛出IOException;相反,异常情况只是设置了一个内部标志,可以通过checkError方法进行测试。或者,可以创建打印流,以便自动刷新。 打印流打印的所有字符都使用平台的默认字符编码转换为字节。PrintWriter类应用于需要写入字符而不是字节的情况。

null

类别声明

public class PrintStream
  extends FilterOutputStream
    implements Appendable, Closeable

领域

 protected OutputStream out:This is the output stream to be filtered. 

构造函数和描述

  • 打印流(文件): 使用指定的文件创建新的打印流,无需自动换行。
  • PrintStream(文件,字符串csn): 使用指定的文件和字符集创建新的打印流,无需自动换行。
  • 打印流(输出流输出): 创建一个新的打印流。
  • PrintStream(OutputStream out,布尔自动刷新): 创建一个新的打印流。
  • PrintStream(OutputStream out、布尔自动刷新、字符串编码) :创建新的打印流。
  • PrintStream(字符串文件名): 使用指定的文件名创建新的打印流,无需自动换行。
  • PrintStream(字符串文件名,字符串csn): 使用指定的文件名和字符集创建新的打印流,无需自动换行。

方法:

  • 打印流附加(字符c): 将指定的字符追加到此输出流。
    Syntax :public PrintStream append(char c)
    Parameters:
    c - The 16-bit character to append
    Returns:
    This output stream
  • PrintStream append(字符序列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
  • 打印流附加(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
  • 受保护的无效clearError(): 清除此流的内部错误状态。
    Syntax :protected void clearError()
    
  • void close(): 关闭小溪。
    Syntax :public void close()
    Overrides:
    close in class FilterOutputStream
  • 无效刷新(): 冲水。
    Syntax :public void flush()
    Overrides:
    flush in class FilterOutputStream
  • PrintStream格式(语言环境l、字符串格式、对象…参数): 使用指定的格式字符串和参数将格式化字符串写入此输出流。
    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格式(字符串格式、对象…参数): 使用指定的格式字符串和参数将格式化字符串写入此输出流。
    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 
  • 无效打印(布尔b): 打印一个布尔值。
    Syntax :public void print(boolean b)
  • 无效打印(字符c): 打印一个字符。
    Syntax :public void print(char c)
  • 无效打印(字符[]s): 打印一组字符。
    Syntax :public void print(char[] s)
    
  • 无效打印(双d): 打印双精度浮点数。
    Syntax :public void print(double b)
    
  • 无效打印(浮动f): 打印一个浮点数。
    Syntax :public void print(float f)
  • 无效打印(int i): 打印一个整数。
    Syntax :public void print(int i)
  • 无效打印(长l): 打印一个长整数。
    Syntax :public void print(long l)
  • 无效打印(对象obj): 打印一个对象。
    Syntax :public void print(Object obj)
  • 无效打印(字符串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伊奥。Java |集合2中的Printstream类

本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享