Java プリントストリーム

WBOY
リリース: 2024-08-30 16:09:47
オリジナル
583 人が閲覧しました

Java PrintStream には、多くのデータ値の表現を印刷する機能があり、別の出力ストリームに機能を追加します。印刷ストリームの特徴は、他の入力ストリームのように IOException をスローしないこと、および checkError メソッドを使用してテストできる例外の発生を示すフラグを内部的に設定することです (これは例外的な場合にのみ発生します)。自動的にフラッシュするように作成することもできます。

文字は、プラットフォームの組み込み文字エンコーディングを使用して PrintStream が生成するバイトに変換されます。したがって、この PrintWriter クラスは、int、long などのバイトではなく文字を書き込む必要がある場所で使用されます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

public class PrintStream
extends FilterOutputStream
implements Appendable, Closeable
ログイン後にコピー

示されているように、PrintStream はクラス FilterOutputStream から継承されており、同じクラスから実装されたインターフェイスは Appendable と Closeable です。

Java PrintStream のコンストラクター

以下は、PrintStream 関数で使用されるコンストラクターと説明です。

  • PrintStream(File file, String csn): これにより、自動フラッシュなしで、指定されたファイルと文字セットを使用して新しい印刷ストリームが作成されます。
  • PrintStream(File file): これにより、指定されたファイルを使用して新しい印刷ストリームも作成されます。これにも自動フラッシュはありません。
  • PrintStream(OutputStream out, boolean autoFlush): これにより、新しい印刷ストリームも作成されます。
  • PrintStream(OutputStream out): パラメーターを 1 つだけ受け入れ、新しい印刷ストリームを作成します。
  • 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 が発生した場合にのみ、ブール値 true を返します
InterruptedIOException などの他の例外がある場合、または setError メソッドが呼び出された場合は false を返します。

5. protected void clearError(): このメソッドは、ストリームの内部エラー状態をクリアするために使用されます。
構文:

6. protected void clearError()

7. void flash(): 戻りパラメータのない別の関数であり、ストリームをフラッシュするために使用されます。
構文:

8. public void flash(): このメソッドは、クラス FilterOutputStream

のフラッシュ関数をオーバーライドします。

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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!