> Java > java지도 시간 > 본문

자바 IO예외

王林
풀어 주다: 2024-08-30 16:13:56
원래의
881명이 탐색했습니다.

Java IOException 또는 IOException은 일반적으로 데이터 스트림, 파일 시스템 및 직렬화 등을 통해 일부 시스템 입력 및 출력에 대한 도움말을 제공합니다. 이는 실제로 IOException을 반환하는 java.util.scanner Java 클래스의 메서드입니다. 이는 일부 Scanner의 기본 Readable에 의해 던져진 마지막 항목입니다. 이 IOException 메소드는 그러한 예외가 실제로 존재하지 않는 경우에만 NULL 값을 반환합니다.

광고 이 카테고리에서 인기 있는 강좌 Java IO 튜토리얼

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

구문:

Public IOException ioException()
로그인 후 복사

IOException의 반환 값:

IOException 메소드/함수는 스캐너의 읽기 가능 항목에 의해 실제로 발생하는 마지막 예외를 반환합니다.

Java에서 IOException은 어떻게 작동하나요?

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 중 하나를 구성하는 데 도움이 됩니다.

4. IOException(IntPtr, JniHandleOwnership): 이 생성자는 일부 JNI 개체의 관리되는 표현을 만드는 데 도움이 되며 런타임은 이를 호출합니다. IntPtr에는 개체 참조 목적으로 일부 JNI(Java Native Interface)가 포함됩니다. JniHandleOwnership 매개변수는 javaReference.

처리를 나타냅니다.

5. IOException(String, Throwable): 생성자는 자세한 메시지 및 원인 채우기와 함께 새 클래스 인스턴스 중 하나를 구성하는 데 도움이 됩니다. 여기서 String 파라미터는 메시지이고, Throwable 파라미터는 원인입니다.

Java IOException 구현 예시

다음은 Java IOException의 예입니다.

예시 #1

이것은 매개 변수를 사용하지 않고 Java 프로그래밍 언어로 일부 Scanner 클래스/클래스의 ioException() 메서드/함수 그림을 구현하는 Java 프로그램 예입니다. 여기서 처음에는 Java 프로그래밍 언어의 모든 라이브러리를 사용하기 위해 java util을 가져옵니다. 그런 다음 예외 발생 개념을 사용하여 pavankumarsake1 클래스가 생성됩니다. 그런 다음 문자열 값을 사용하여 s1 문자열 변수가 생성됩니다. 그런 다음 scanner1 변수는 s1 문자열 변수의 도움으로 생성됩니다. 그런 다음 scanner.nextLine()을 생성하여 새 줄을 인쇄한 다음 scanner.ioException()을 사용하여 IO Exception이 있는지 여부를 확인합니다. 그러면 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();
}
}
로그인 후 복사

출력:

자바 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:

자바 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:

자바 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 :

자바 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.

위 내용은 자바 IO예외의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!