首頁 > Java > java教程 > 主體

Java列印流

WBOY
發布: 2024-08-30 16:09:47
原創
581 人瀏覽過

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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!