目次
Constructors of Java PrintWriter Class
Methods of Java PrintWriter Class
Examples of Java PrintWriter
Example #2
Conclusion

Java プリントライター

Aug 30, 2024 pm 04:09 PM
java

The java PrintWriter class is used to print objects in the formatted representation to the text output stream. The PrintWriter class is a built-in class in java that defines the java.io.PrintWriter package. The Writer class is a superclass of the PrintWriter. The PrintWriter class enables to write the formatted object to the underlying Writer class, for example, to write int, double, long and other primitive object or data as in the text, not in the values of the bytes. The PrintWriter class implements all the print method as the PrintStream except the methods which write the raw bytes. As the PrintWriter class intended to write the text, it is useful to generate reports to mix the numbers and text.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

The following is the declaration for java.io. PrintWriter class:

public class PrintWriter extends Writer
{
// Constructors and methods of the PrintWriter class
}
ログイン後にコピー

The above is the syntax of the PrintWriter, where it is extended to the Writer class.

Constructors of Java PrintWriter Class

Given below are the constructors mentioned:

  • PrintWriter(File file): These constructs create the new instance of PrintWriter, with the specified file and which is not automat line flushing.
  • PrintWriter(File file, String ch): These constructs create the new instance of PrintWriter, with the specified file, charset, and not automatic line flush.
  • PrintWriter(OutputStream out): These constructs create the new instance of PrintWriter from an existing OutputStream and which is not automat line flush.
  • PrintWriter(OutputStream out, booleanautoFlush): These constructs create the new instance of PrintWriter from an existing OutputStream.
  • PrintWriter(String fName): These constructs create the new instance of PrintWriter with the specified file name and which is not automat line flush.
  • PrintWriter(String fileName, String csn): These constructs create the new instance of PrintWriter with the specified file name and charset, and it is not automat line flush.
  • PrintWriter(Writer out): These constructs create the new instance of PrintWriter, which is not an automat line flush.
  • PrintWriter(Writer out, booleanautoFlush): These constructs create the new instance of PrintWriter.

Methods of Java PrintWriter Class

Given below are the methods:

  • public void print( Object obj): This method prints an object.
  • public void println(boolean x): This method prints the boolean value.
  • public void println(char[] x): This method prints an array of characters.
  • public void println(int x): This method prints an integer. Similar methods to prints for all different primitive objects.
  • public PrintWriterappend(char ch): This method appends the pass character to the writer.
  • public PrintWriterappend(CharSequencechseq): This method appends the specified character sequence to the writer.
  • public PrintWriterappend(CharSequencech, int start, int end): This method appends a subsequence of pass character to the writer.
  • public booleancheckError(): This method checks its error state and flushes the stream.
  • public protected void clearError(): This method clears an internal error state of the stream.
  • public protected void setError(): This method indicates that an error occurs.
  • public PrintWriterformat(String format, Object..args): This method writes the formatted string to this writer with the passed format string and arguments.
  • public void flush(): This method flushes the stream.
  • public void close(): This method closes the stream.

Examples of Java PrintWriter

Given below are the examples mentioned:

Example #1

Here we create a PrintWriter object by using the PrintWriter class constructor and pass the file name to write in it.

Code:

package p1;
import java.io.File;
import java.io.PrintWriter;
public class Demo
{
public static void main( String[] arg) {
try {
//create the new instance of PrintWriter with the specified file
PrintWriter pw =null;
pw = new PrintWriter(new File("D:\\data.txt"));
pw.write("This ia an example text for PrintWriter."+"\n");
pw.write("This ia an example number for PrintWriter :" +(int)563+ ".");
pw.flush();
pw.close();
System.out.println("Print writer done. you can open the file.");
}catch(Exception e)
{
System.out.println(e);
}
}
}
ログイン後にコピー

Output:

Java プリントライター

When we open the file, we can see the content of the file as below:

Java プリントライター

As in the above code, the file “data.txt” is opened to write some data with the help of PrintWriter class and its constructor.

Example #2

Here we create a PrintWriter object of a console that is System.out by using the PrintWriter class constructor and write on the console.

Code:

package p1;
import java.io.File;
import java.io.PrintWriter;
import java.util.Locale;
public class Demo
{
public static void main( String[] arg) {
String str="Hello";
String str2=" World";
char ch='H';
try {
//create the new instance of PrintWriter to System.out
PrintWriter pw =null;
pw = new PrintWriter(System.out);
pw.print("print(inti) : "+ 123);
pw.println();
pw.print("print(float f) : " +34.65f);
pw.println();
pw.print("print(boolean b) : " +true);
pw.println();
pw.print("print(char ch) : "+ch);
pw.println();
pw.print("print(String s) : "+str);
pw.println();
pw.print("print(Object Obj) : " +pw);
pw.println();
pw.print("append(CharSequencecsq) : ");
pw.append(str);
pw.append(str2);
pw.println();
pw.println("checkError() : " +pw.checkError());
pw.print("format() : ");
pw.format(Locale.CHINA, " This is an china formatted example for %s text.",str);
pw.flush();
pw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
ログイン後にコピー

Output:

Java プリントライター

As in the above code, writing some of the data we can see in the output to the out console (System.out) with PrintWriter class constructor and methods.

Conclusion

The PrintWriter class is a built-in class in java that is defined in the java.io.PrintWriter package, and it is used to print an object in the formatted representation to the text output stream. The PrintWriter class is the subclass of the Writer class.

以上がJava プリントライターの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

PHP対Python:違いを理解します PHP対Python:違いを理解します Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

PHP:Web開発の重要な言語 PHP:Web開発の重要な言語 Apr 13, 2025 am 12:08 AM

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

Java 8 Stream Foreachから休憩または戻ってきますか? Java 8 Stream Foreachから休憩または戻ってきますか? Feb 07, 2025 pm 12:09 PM

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

PHP対その他の言語:比較 PHP対その他の言語:比較 Apr 13, 2025 am 12:19 AM

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。

PHP対Python:コア機能と機能 PHP対Python:コア機能と機能 Apr 13, 2025 am 12:16 AM

PHPとPythonにはそれぞれ独自の利点があり、さまざまなシナリオに適しています。 1.PHPはWeb開発に適しており、組み込みのWebサーバーとRich Functionライブラリを提供します。 2。Pythonは、簡潔な構文と強力な標準ライブラリを備えたデータサイエンスと機械学習に適しています。選択するときは、プロジェクトの要件に基づいて決定する必要があります。

PHPの影響:Web開発など PHPの影響:Web開発など Apr 18, 2025 am 12:10 AM

phphassiblasifly-impactedwebdevevermentandsbeyondit.1)itpowersmajorplatformslikewordpratsandexcelsindatabase interactions.2)php'sadaptableability allowsitale forlargeapplicationsusingframeworkslikelavel.3)

PHP:多くのウェブサイトの基礎 PHP:多くのウェブサイトの基礎 Apr 13, 2025 am 12:07 AM

PHPが多くのWebサイトよりも優先テクノロジースタックである理由には、その使いやすさ、強力なコミュニティサポート、広範な使用が含まれます。 1)初心者に適した学習と使用が簡単です。 2)巨大な開発者コミュニティと豊富なリソースを持っています。 3)WordPress、Drupal、その他のプラットフォームで広く使用されています。 4)Webサーバーとしっかりと統合して、開発の展開を簡素化します。

PHP対Python:ユースケースとアプリケーション PHP対Python:ユースケースとアプリケーション Apr 17, 2025 am 12:23 AM

PHPはWeb開発およびコンテンツ管理システムに適しており、Pythonはデータサイエンス、機械学習、自動化スクリプトに適しています。 1.PHPは、高速でスケーラブルなWebサイトとアプリケーションの構築においてうまく機能し、WordPressなどのCMSで一般的に使用されます。 2。Pythonは、NumpyやTensorflowなどの豊富なライブラリを使用して、データサイエンスと機械学習の分野で驚くほどパフォーマンスを発揮しています。

See all articles