首页  >  文章  >  Java  >  Java打印流

Java打印流

WBOY
WBOY原创
2024-08-30 16:09:47583浏览

Java PrintStream 能够打印许多数据值的描述,并向不同的输出流添加功能。打印流的特殊之处在于它不会像其他输入流那样抛出 IOException,并且它们在内部设置一个标志来指示发生异常,可以使用 checkError 方法进行测试(这只发生在异常情况下)。它也可以被创建为自动刷新自身。

字符将转换为 PrintStream 使用平台内置字符编码生成的字节。因此,这个 PrintWriter 类用于需要为 int、long 等写入字符而不是字节的地方

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

public class PrintStream
extends FilterOutputStream
implements Appendable, Closeable

PrintStream,如图所示,继承自FilterOutputStream类,其实现的接口有Appendable和Closeable。

Java PrintStream 的构造函数

以下是 PrintStream 函数使用的构造函数和说明:

  • PrintStream(File file, String csn): 这将使用给定的文件和字符集创建一个新的打印流,而无需任何自动刷新。
  • PrintStream(文件文件): 这还会使用指定文件创建一个新的打印流。这也没有自动冲洗功能。
  • PrintStream(OutputStream out, boolean autoFlush):这也会创建一个新的打印流。
  • PrintStream(OutputStream out):仅接受单个参数并创建一个新的打印流。
  • PrintStream(String fileName): 接受文件名作为输入参数并创建一个新的打印流,而不提供自动行刷新。
  • PrintStream(OutputStream 输出,布尔值 autoFlush,字符串编码):
    这接受如图所示的 3 个输入参数并创建一个新的打印流。
  • PrintStream(String fileName, String chs): 这还会创建一个新的打印流,该打印流不会使用给定的文件名和字符集自动进行行刷新。

使用方法列表

1。 PrintStream append(char a): 该方法用于将给定的字符附加到输出流

语法:

public PrintStream append(char a)

所需参数:将输入参数作为字符类型 a – 附加到 16 位字符。
返回:输出流

2。 PrintStream appfin(CharSequence chs, int st, int fin): 此函数需要 3 个参数,并将给定的字符序列附加到此输出流。

语法:

public PrintStream appfin(CharSequence chs,
int st,
int fin)

所需参数:

  • chs: 输入字符序列由此获取,子序列将附加在此处。
  • st: 这表示子序列中第一个字符的索引。
  • fin: 这表示子序列中最后一个字符的索引。
  • 返回:输出流。
  • 抛出: 异常,如 IndexOutOfBoundsException。

3。 PrintStream append(CharSequence chs): 此方法用于将给定字符序列的子序列附加到此输出流。
语法:

public PrintStream append(CharSequence chs)

所需参数:

  • chs: 必须附加的字符序列。
  • 返回:输出流。

4。 Boolean checkError(): 这用于刷新流并获取其错误状态。

语法:

public boolean checkError()

返回参数: 仅当该流遇到 IOException
时才返回布尔真值 如果有任何其他异常(例如 InterruptedIOException),或者调用了 setError 方法,则返回 false。

5。 protected void clearError(): 此方法用于清除流的任何内部错误状态。
语法:

6。 protected void clearError()

7。 voidlush(): 另一个没有返回参数的函数,用于刷新流。
语法:

8。 public voidlush(): 该方法重写了FilterOutputStream类的flush函数

9。 void close(): 用于关闭流的基本方法。

语法:

public void close()

该方法重写了 FilterOutputStream 类的 close() 函数

10. PrintStream format(Locale loc, String fr, Object… arg): This function is used to write a string that is formatted to the output stream using the given format string and parameters.

Syntax:

public PrintStream format(Locale loc,
String fr,
Object... arg)

Parameters required:

  • loc: The locale we use during formatting. If this value is null, then no need to apply localization.
  • arg: All the formal specifiers use these arguments as a reference in the format string. The arguments passed here can range from zero to many.
  • Return parameters: The output stream. Throws 2 kinds of exception IllegalFormatException and NullPointerException

11. PrintStream format(String for, Object… args): Used to write a formatted string to the output stream using the given format string and parameters.

Syntax:

public PrintStream format(String for,
Object... args)

Parameters required:

  • for: Format string as per the syntax
  • args: Input arguments as described, and they may range from zero to many
  • Return parameters: The output stream. Throws 2 kinds of exception IllegalFormatException and NullPointerException

Example to Implement Java PrintStream

Below is an example of Java PrintStream. First, let us undertake a basic example to understand the above discussed different methods of PrintStream.

Code:

import java.io.*;
import java.util.Locale;
//Java code to display different methods of Printstream
public class Main
{
public static void main(String args[]) throws FileNotFoundException
{
// Creating an output file to write the output
FileOutputStream file=new FileOutputStream("output.txt");
// Creating object of PrintStream
PrintStream op=new PrintStream(file);
String str="Example";
// Writing below to output.txt
char a[]={'F','I','R','S','T'};
// Example for print(boolean b) method
op.print(true);
op.println();
// Example for print(int a) method
op.print(1);
op.println();
// Example for print(float f) method
op.print(5.10f);
op.println();
// Example for print(String str) method
op.print("Example code for PrintStream methods");
op.println();
// Example for print(Object ob) method
op.print(file);
op.println();
// Example for append(CharSequence chs) method
op.append("Append method");
op.println();
//Example for checkError() method
op.println(op.checkError());
//Example for format() method
op.format(Locale.US, "This is a %s program", str);
//Example for flush method
op.flush();
//Example for close method
op.close();
}
}

Output:

Java打印流

Explanation: This example generates an output file, and we are displaying all the method related outputs by writing them into the output.txt file. This creates a file if it does not exist, and hence the output will not be visible in the IDE. We are first creating a PrintStream object here and then using that to showcase all the functioning of methods like print(boolean b), print(int I), print(float f), print(String s) and other methods as shown in the code.

Conclusion

Hence as discussed above, a PrintStream in java that is basically used to write formatted data to the output stream. The naming is done as per its functionality that it formats the primitive values like int, long into text like as to when they will look when displayed on a screen.

以上是Java打印流的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn