首頁  >  文章  >  Java  >  Java IO異常

Java IO異常

王林
王林原創
2024-08-30 16:13:56883瀏覽

Java IOException 或 IOException 通常為一些系統輸入和輸出提供協助,這些輸入和輸出是透過資料流、檔案系統和序列化等。這是 java.util.scanner java 類別的方法,它實際上會傳回 IOException,它是某些 Scanner 的底層 Readable 拋出的最後一個。只有當實際上不存在此類異常時,此 IOException 方法才會傳回 NULL 值。

廣告 該類別中的熱門課程 Java IO 教學

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

Public IOException ioException()

IOException 的回傳值:

IOException方法/函數將傳回最後一個異常,該異常實際上是由掃描器的可讀/可讀拋出的。

IOException 在 Java 中如何運作?

Java 語言的 IOException 的工作原理是為資料流、序列化和檔案系統中可用的某些輸入和輸出提供一些幫助。 IoException() 方法/函數的工作原理是傳回最後拋出的 Scanner IOException 的底層可讀概念。只有當根本沒有異常可用時,IOException() 方法/函數才會傳回 NULL 值。

如何避免 IOException?

使用 Java 程式語言的 Try/Catch 概念,我們可以處理 IOException 。這是避免 IOException 的概念。

Java 中 IOException 的建構子

通常,建構函式有助於在物件建立後立即初始化物件。它在語法上與特定方法/函數相似/相同,但構造函數有一些差異。這與沒有傳回類型的類別相同。實際上,不需要明確呼叫任何建構函數,這些構造函數會在實例化時自動呼叫。這些構造函數會拋出一些異常。

1。 IOException(): 這是建構新 IOException 之一及其堆疊追蹤的普通建構函數,需要填入。

2。 IOException(Throwable): Throwable 建構子有助於建構一個新的類別實例及其詳細原因,將會填寫。這裡“Throwable”參數就是原因。

3。 IOException(String): IOException() 的 String 建構子有助於建構新的 IOException 之一及其堆疊追蹤和詳細訊息:filled。

4。 IOException(IntPtr, JniHandleOwnership): 此建構子有助於建立某些 JNI 物件的託管表示,並且在執行時會呼叫它們。 IntPtr 將包含一些用於物件參考目的的 Java 本機介面 (JNI)。 JniHandleOwnership 參數表示處理 javaReference。

5。 IOException(String, Throwable): 建構子有助於建構新的類別實例之一以及一些詳細訊息和原因填充。這裡,String參數是訊息,Throwable參數是原因。

Java IOException 的實作範例

以下是 Java IOException 的範例:

範例#1

這是在Java程式語言中不使用任何參數實作某些Scanner類別的ioException()方法/函數的說明的java程式範例。首先,導入 java util 以使用 Java 程式語言的所有函式庫。然後使用異常拋出概念創建 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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 算術異常下一篇:Java 算術異常