Java IO例外

王林
リリース: 2024-08-30 16:13:56
オリジナル
881 人が閲覧しました

Java IOException または IOException は、通常、データ ストリーム、ファイル システム、シリアル化などを通じて、システムの入出力に対するヘルプを提供します。これは、実際に IOException を返す java.util.scanner Java クラスのメソッドです。これは、Scanner の基礎となる Readable によってスローされる最後のものです。この IOException メソッドは、そのような例外が実際に存在しない場合にのみ NULL 値を返します。

広告 このカテゴリーの人気コース Java IO チュートリアル

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

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

構文:

Public IOException ioException()
ログイン後にコピー

IOException の戻り値:

IOException メソッド/関数は、スキャナーの読み取り可能/読み取り可能によって実際にスローされる最後の例外を返します。

Java での IOException の仕組み

Java 言語の IOException は、データ ストリーム、シリアル化、およびファイル システム全体で使用できる入力と出力の一部にヘルプを提供することによって機能します。 IoException() メソッド/関数は、最後にスローされたスキャナーの基礎となる読み取り可能な概念である IOException を返すことによって機能します。 IOException() メソッド/関数は、例外がまったく利用できない場合にのみ NULL 値を返します。

IOException を回避するには?

Java プログラミング言語の Try/Catch 概念を使用すると、 IOException を処理できます。これはIOExceptionを回避するための概念です。

Java の IOException のコンストラクター

通常、コンストラクターは、オブジェクトの作成直後にオブジェクトを初期化するのに役立ちます。構文的には特定のメソッド/関数と似ています/同じですが、コンストラクターにはいくつかの違いがあります。それは、戻り値の型を持たないクラスと同じものを持っています。実際には、コンストラクターを明示的に呼び出す必要はなく、インスタンス化時に自動的に呼び出されます。これらのコンストラクターはいくつかの例外をスローします。

1. IOException(): これは、新しい IOException とそのスタック トレースの 1 つを構築する通常のコンストラクターであり、入力されます。

2. IOException(Throwable): Throwable コンストラクターは、新しいクラス インスタンスの 1 つとその詳細な原​​因を構築するのに役立ちます。詳細な原因が埋められます。ここでは、「Throwable」パラメーターが原因です。

3. IOException(String): IOException() の String コンストラクターは、新しい IOException の 1 つをそのスタック トレースおよび詳細メッセージとともに構築するのに役立ちます:filled.

4. IOException(IntPtr, JniHandleOwnership): このコンストラクターは、一部の JNI オブジェクトの管理された表現の作成に役立ち、ランタイムはそれらを呼び出します。 IntPtr には、オブジェクト参照を目的とした Java Native Interface (JNI) が含まれます。 JniHandleOwnership パラメータは、javaReference.

の処理を​​示します。

5. IOException(String, Throwable): コンストラクターは、詳細メッセージおよび原因の補完とともに、新しいクラス インスタンスの 1 つを構築するのに役立ちます。ここで、String パラメータはメッセージであり、Throwable パラメータは原因です。

Java IOException の実装例

以下は Java IOException の例です:

例 #1

これは、パラメータを使用せずに、Java プログラミング言語の一部のスキャナ クラスの ioException() メソッド/関数の図を実装する Java プログラムの例です。ここではまず、Java プログラミング言語のすべてのライブラリを使用するために java util をインポートします。次に、例外スローの概念を使用して pavankumarsake1 クラスが作成されます。次に、文字列値を使用して s1 文字列変数が作成されます。次に、s1 文字列変数を使用して、scanner1 変数が作成されます。次に、scanner.nextLine() を作成して新しい行を出力し、scanner.ioException() を使用して IO 例外が存在するかどうかを確認します。その後、scanner.close() 関数を使用してスキャナーが閉じられます。

コード:

import java.util.*;
public class pavansake1 {
public static void main(String[] args)
throws Exception
{
System.out.println(" \n ");
String s1 = "Hey! I'am from EDUCBA";
Scanner scanner1 = new Scanner(s1);
System.out.println("" + scanner1.nextLine());
System.out.println("" + scanner1.ioException());
scanner1.close();
}
}
ログイン後にコピー

出力:

Java IO例外

Example #2

This is the Java example of illustrating the ioException() method along with some of its constructors of the Scanner class in the Java Programming Language without the parameter mentioning. Here at first, java.util.* is imported to import all the functions of the library. Then public class “pavansake1” is created by throwing the exception concept. In the throwing exception, a string s11 is created with some string value. Then a scanner variable is created with the help of the s11 variable to use it as an alternative similar one. Then the ioException() variable is used to show only if there is some IO exception.

Code:

import java.util.*;
public class pavansake1 {
public static void main(String[] argv)
throws Exception
{
System.out.println("\n");
String s11 = "EDUCBA EDUCBA!!!";
Scanner scanner11 = new Scanner(s11);
System.out.println("" + scanner11.nextLine());
System.out.println("" + scanner11.ioException());
scanner11.close();
}
}
ログイン後にコピー

Output:

Java IO例外

Example #3

This is the example of implementing the IOException along with the constructor in it, but this example will provide a compilation error due to an issue. Here a class “Employee1” is created whose constructor will throw one of the IOException, and it is instantiating the class which is not handling our exception. So the compilation leads to the compile run time error. Some Java libraries are imported at first, and then private class and public class are created to handle the IOException, but here exception is not handled, so the error occurred.

Code :

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Employee1{
private String name1;
private int age1;
File empFile1;
Employee1(String name1, int age1, String empFile1) throws IOException{
this.name1 = name1;
this.age1 = age1;
this.empFile1 = new File(empFile1);
new FileWriter(empFile1).write("Employee name is "+name1+"and age is "+age1);
}
public void display(){
System.out.println("Name: "+name1);
System.out.println("Age: "+age1);
}
}
public class ConstructorExample1 {
public static void main(String args[]) {
String filePath1 = "samplefile.txt";
Employee emp1 = new Employee("pksake", 25, filePath);
}
}
ログイン後にコピー

Output:

Java IO例外

Example #4

This is the example that is very much similar to example 3. To make example 3 to work, we are wrapping the line’s instantiation within the try-catch or the throw exception.

Code:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Employee1{
private String name1;
private int age1;
File empFile1;
Employee1(String name1, int age1, String empFile1) throws IOException{
this.name1 = name1;
this.age1 = age1;
this.empFile1 = new File(empFile1);
new FileWriter(empFile1).write("Employee name is "+name1+"and age is "+age1);
}
public void display(){
System.out.println("Name: "+name1);
System.out.println("Age: "+age1);
}
}
public class Employee11 {
public static void main(String args[]) {
String filePath1 = "file1.txt";
Employee1 emp1 = null;
try{
emp1 = new Employee1("pksake", 26, filePath1);
}catch(IOException ex1){
System.out.println("The specific file will not be found");
}
emp1.display();
}
}
ログイン後にコピー

Output :

Java IO例外

Conclusion

I hope you have learned the definition of Java IOException and its syntax and explanation, How the IOException works in Java Programming Language and its constructors, Java IOException examples, and How to avoid the IOException, etc.

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

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