Javaコンソール

PHPz
リリース: 2024-08-30 16:10:15
オリジナル
352 人が閲覧しました

Java コンソールは、文字ベースのエントリを読み取るコンソール デバイスを起動するために、JDK 1.6 で導入された java.io パッケージに存在するクラスです。このクラスは、ユーザーの入力を読み取り、コンソールに書き戻すために使用されます。以下の 2 つの理由により、バッファされた Reader および Writer よりもこれが推奨されます:-

  1. System クラスを使用して新しい Console インスタンスを呼び出し、Console クラスには独自のコンストラクターがないため、アクセスが簡単です。
  2. 画面に表示されないパスワードなどのセキュリティ資格情報の入力を行うのに役立ちます。

構文:

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

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

public final class Console extends Object implements Flushable
ログイン後にコピー

Java でコンソール クラスはどのように機能しますか?

Console クラスは、コンソールから入力を読み取るか、コンソールに出力を書き込むために使用されます。 Console クラスにはコンストラクターがなく、System クラスを使用してインスタンス化されるため、使いやすくなります。また、Console クラスは、ユーザーがセキュリティ資格情報などの入力を画面に表示されないようにするのに役立つため、より安全な方法でコンソールから入力を読み取ります。したがって、コンソール クラスは、セキュリティ認証情報を使用する必要がある場合に非常に役立ちます。

Java コンソールのメソッド

以下は Java コンソールのメソッドです:

1. public PrintWriterwriter(): このメソッドは、実行中のコンソール オブジェクトに関連付けられている PrintWriter の一意のインスタンスを取得するために使用されます。このメソッドにはパラメータは必要ありません。

2. public void flash(): Console クラスは、コンソール インスタンスをフラッシュするためのこのメソッド を提供します。また、出力がバッファーに存在する場合は、すぐに出力を印刷するように注意します。この関数を呼び出すのにパラメータは必要ありません。

3. public Reader Reader(): このメソッドは、現在のコンソールに関連付けられた Reader クラスの一意のインスタンスを提供します。 Reader クラスのインスタンスは、コンソールから文字を読み取るための入力として与えられます。

4. public Console format( StringformatVar, Object … ages): このメソッドは、指定されたフォーマット文字列と引数を使用して、指定されたフォーマットされた文字列をコンソール出力ストリームに送信するのに役立ちます。

パラメータ:

  • formatVar: フォーマット文字列は、指定された文字列と引数を使用してコンソール出力に出力される必要があります。
  • args: これらの引数は、formatVar.
  • で渡される文字列内の形式指定子によって参照されます。

コンソール インスタンスは、この関数によって出力として送信され、書式設定された文字列が出力されます。指定された形式の構文が正しくない場合、このメソッドは IllegalFormatException をスローします。

5. public Console printf( StringformatVar, Object … agrs): このメソッドは、コンソール画面に渡された、指定された書式引数を使用して書式設定された文字列を出力するのに役立ちます。

  • formatVar – フォーマット文字列は、指定された文字列と引数を使用してコンソール出力に出力される必要があります。
  • args – これらの引数は、formatVar.
  • で渡される文字列内の形式指定子によって参照されます。

コンソール インスタンスは、この関数によって出力として送信され、書式設定された文字列が出力されます。指定された形式の構文が正しくない場合、このメソッドは IllegalFormatException をスローします。

6. public String readLine( StringformatVar, Object … age): このメソッドは、書式設定されたプロンプトを画面に表示し、ユーザーからの 1 行の入力を読み取るために使用されます。

コンソールからのプロンプトによって読み取られた 1 行のうち、行末文字を除いたものがメソッドによって返されます。何も入力されない場合、このメソッドは null を返します。指定された形式の構文が正しくない場合、このメソッドは IllegalFormatException をスローします。また、IOError は I/O エラーに対して発生します。

7. public String readLine(): このメソッドは、コンソールからユーザーからの単一行入力を読み取るために使用されます。エスケープ文字が渡された場合は除外されます。

この文字列はメソッドによって返されるか、行末に達した場合は null が返されます。このメソッドではプロンプトが表示されないため、パラメーターは必要ありません。このメソッドを使用すると、I/O エラーに対して IOERROR が発生します。

8. public char[] readPassword( String formatVar, Object … age): このメソッドは、入力時に画面に表示されないように、セキュア モードを使用して文字を読み取る書式設定されたプロンプトを画面に表示するために使用されます。

  • formatVar: The format string needs to be printed on the console output using the specified string and arguments.
  • args: These arguments are referred to by the format specifiers in the string passed in formatVar.

Character Array containing the password is sent as output by this function or null in case the end-of-line is reached. The returned character array excludes line-termination characters. This method throws IllegalFormatException in case the format specified does not have the correct syntax. This method IOERROR occurs for any I/O errors.

9. public char[] readPassword(): This method is used to read password string from the console in a secure mode without displaying any prompt. There are no parameters that need to be passed. The string of characters containing the password is returned excluding the line-termination characters or null in case the end-of-line is reached.

Examples to Implement of Java Console

Below are the examples of Java Console:

Example #1

Code:

import java.io.Console;
public class ConsolePrac {
public static void main(String[] args) {
Console consoleObj = null;
try {
consoleObj = System.console();
if (consoleObj != null) {
String person = consoleObj.readLine("Please enter your Name: ");
System.out.println("Welcome " + person);
System.out.println("Please check the below  result");
String fmt = "%2$8s %1$10s %3$3s%n";
consoleObj.format(fmt, "Name", "RollNo", "MArks");
consoleObj.format(fmt, "-----", "-----", "-----");
consoleObj.format(fmt, "Raj", "1212", "75");
consoleObj.printf(fmt, "Meeta", "1213", "67");
consoleObj.printf(fmt, "Sanjana", "1215", "89");
consoleObj.printf(fmt, "Pawan", "1214", "80");
}
consoleObj.flush();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
ログイン後にコピー

Output:

Javaコンソール

Example #2

Code:

import java.io.Console
import java.util.Scanner;
import java.io.PrintWriter;
public class ConsolePrac {
public static void main(String[] args) {
Console consoleObj = null;
PrintWriter PWObj = null;
String fmt1 = "%1$6s %2$5s %3$10s%n";
String fmt2 = "%1$8s %2$10s %3$5s %4$5s %5$10s%n";
Scanner scanObj = null;
try {
consoleObj = System.console();
if (consoleObj != null) {
System.out.print("Please enter your Name: ");
scanObj = new Scanner(consoleObj.reader());
String person = scanObj.next();
String empID = consoleObj.readLine(fmt1, "Please","enter","EmployeeID: ");
char[] pwd = consoleObj.readPassword("Please enter your Password: ");
char[] ans1 = consoleObj.readPassword(fmt2,"Security","question- ","Enter","your","Mothers'name: ");
PWObj = consoleObj.writer();
PWObj.println("Welcome "+person +" "+empID);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
ログイン後にコピー

Output:

Javaコンソール

Note: The format string’s arguments must be the same as the String to be displayed. Please see below 2 scenarios:

Case 1: The arguments are more than specified in the format string.

String fmt= "%1$30s %2$10s"
String empID = consoleObj.readLine(fmt1, "Please","enter","your","EmployeeID: ");
ログイン後にコピー

In the above case, only the first 2 strings will be printed, and others are ignored.

Case 2:  In case the arguments specified are less than a missing argument exception is thrown.

String fmt= "%1$30s %2$10s" ,"%3$10s%n"
String empID = consoleObj.readLine(fmt1, "Please","enter”);
ログイン後にコピー

Javaコンソール

Note: Console program must be run using command line since eclipse gives exception while running this program.

Conclusion

Console class is one of the great utility introduced with JDK 1.6 that helps to read the input from a console instance in a secure manner. It also provides methods to write output to the console screen.

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

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