首页 > Java > java教程 > 正文

Java IO异常

王林
发布: 2024-08-30 16:13:56
原创
881 人浏览过

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!