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

此类为文本输出流提供对象的打印格式表示。它实现了PrintStream中的所有打印方法。它不包含写入原始字节的方法,对于原始字节,程序应该使用未编码的字节流。 与PrintStream类不同,如果启用了自动刷新,则只会在调用println、printf或format方法之一时执行,而不是在输出换行符时执行。这些方法使用平台自己的行分隔符概念,而不是换行符。

null

这个类中的方法从不抛出I/O异常,尽管它的一些构造函数可能会抛出I/O异常。客户端可以通过调用checkError()查询是否发生了任何错误。 构造函数和描述

  • PrintWriter(文件): 使用指定的文件创建新的PrintWriter,无需自动换行。
  • PrintWriter(文件,字符串csn): 使用指定的文件和字符集创建新的PrintWriter,无需自动换行。
  • PrintWriter(输出流输出): 从现有的OutputStream创建新的PrintWriter,无需自动换行。
  • PrintWriter(输出流输出,布尔自动刷新): 从现有输出流创建新的PrintWriter。
  • PrintWriter(字符串文件名): 使用指定的文件名创建新的PrintWriter,无需自动换行。
  • PrintWriter(字符串文件名,字符串csn): 使用指定的文件名和字符集创建新的PrintWriter,无需自动换行。
  • PrintWriter(Writer out): 创建新的PrintWriter,无需自动换行。
  • PrintWriter(Writer out,布尔自动刷新): 创建一个新的PrintWriter。

方法:

  • PrintWriter附加(字符c): 将指定的字符追加到此写入程序
    Syntax :public PrintWriter append(char c)
    Parameters:
    c - The 16-bit character to append
    Returns:
    This writer
  • PrintWriter append(字符顺序csq,int start,int end): 将指定的字符序列追加到此写入程序。
    Syntax :public PrintWriter 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 writer
    Throws:
            IndexOutOfBoundsException
  • PrintWriter附加(CharSequence csq): 将指定字符序列的子序列追加到此写入程序。
    Syntax :public PrintWriter append(CharSequence csq)
    Parameters:
            csq - The character sequence to append.
    Returns: This writer
    
  • 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()
    Specified by:close in class Writer
  • 无效刷新(): 冲水。
    Syntax :public void flush()
    Specified by:flush in interface Flushable
    Specified by:flush in class Writer
    
  • PrintWriter格式(语言环境l、字符串格式、对象…参数): 使用指定的格式字符串和参数将格式化字符串写入此编写器。
    Syntax :public PrintWriter 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 writer
    Throws:
            IllegalFormatException
            NullPointerException
  • PrintWriter格式(字符串格式、对象…参数): 使用指定的格式字符串和参数将格式化字符串写入此编写器。
    Syntax :public PrintWriter 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 writer
    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.*;
import java.util.Locale;
//Java program to demonstrate PrintWriter
class PrintWriterDemo {
public static void main(String[] args)
{
String s= "GeeksforGeeks" ;
// create a new writer
PrintWriter out = new PrintWriter(System.out);
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(out);
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, "This is my %s program" , s);
//illustrating flush method
out.flush();
//illustrating close method
out.close();
}
}


Output:
        true14.533GeeksforGeeks
        java.io.PrintWriter@1540e19d
        Geek
        false
        This is my GeeksforGeeks program

下一篇文章: JAVA伊奥。Java | Set 2中的PrintWriter类

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

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

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