logo

Java의 FileNotFoundException

FileNotFoundException 에서 사용할 수 있는 또 다른 예외 클래스는 다음과 같습니다. java.io 패키지. 시스템에서 사용할 수 없는 파일에 액세스하려고 하면 예외가 발생합니다. 이는 컴파일 타임이 아닌 런타임에 발생하고 다음 생성자 중 하나에 의해 발생하므로 확인된 예외입니다.

    무작위액세스파일 파일입력스트림 파일출력스트림
Java의 FileNotFoundException

FileNotFoundException 생성자

FileNotFoundException 클래스에는 다음 두 개의 생성자가 있습니다.

1. 파일NotFoundException()

FileNotFoundException을 생성하고 생성자에 매개변수를 전달하지 않았기 때문에 오류 세부 메시지를 null로 설정합니다.

통사론:

구문은 FileNotFoundException 다음과 같다:

 public FileNotFoundException() 

2. FileNotFoundException(문자열 문자열)

FileNotFoundException을 생성하고 오류 세부 메시지를 설정합니다. str, 생성자에게 전달합니다.

통사론:

구문은 FileNotFoundException 다음과 같다:

 public FileNotFoundException(String str) 

FileNotFoundException 메서드

에서 제공하는 모든 메소드를 제공합니다. java.lang.Throwable 그리고 java.lang.Object 클래스는 이 두 클래스의 하위 클래스이기 때문입니다.

java.lang.Throwable 클래스의 메소드

추가억제됨 (), fillInStackTrace (), getCause (), getLocalizedMessage (), getMessage (), getStackTrace (), getSuppressed (), 초기화 원인 (), printStackTrace (), printStackTrace (), printStackTrace (), setStackTrace (), 그리고 toString ().

java.lang.Object 클래스의 메소드

클론 (), 같음 (), 마무리하다 (), getClass (), 해시 코드 (), 통지하다 (), 모두에게 알리다 (), 그리고 기다리다 ().

이러한 방법에 대해 자세히 알아보려면 다음을 방문하세요.

https://www.javatpoint.com/object-class

https://www.javatpoint.com/post/java-throwable

FileNotFoundException이 발생하는 이유는 무엇입니까?

이 오류가 발생하는 데에는 주로 두 가지 이유가 있습니다. 이 예외가 발생하는 이유는 다음과 같습니다.

int에서 문자열로
  1. 해당 파일에 액세스하려고 하면 시스템에서 해당 파일을 사용할 수 없습니다.
  2. 예를 들어, 파일이 읽기 전용 작업에 사용 가능하고 이를 수정하려고 하면 액세스할 수 없는 파일에 액세스하려고 하면 오류가 발생할 수 있습니다.

몇 가지 예를 들어 위의 두 가지 사항을 하나씩 이해해 보겠습니다.

FileNotFoundExample1.java

 // import required classes and packages package javaTpoint.MicrosoftJava; import java.io.*; // it contains all the input and the output streams // create FileNotFoundExceptionExample1 to undestand the first point. public class FileNotFoundExceptionExample1 { public static void main(String[] args) { // creating an instance of the FileReader class FileReader fileReader = new FileReader('Test.txt'); // create an instance of the BufferedReader and pass the FileReader instance to it. BufferedReader bufferReader = new BufferedReader(fileReader); // declaring an empty string by passing null value String fileData = null; // use while loop to read and print data from buffered reader while ((fileData = bufferReader.readLine()) != null) { System.out.println(fileData); } // closing the BufferedReader object try { bufferReader.close(); } catch (IOException e) { e.printStackTrace(); } } } 

산출:

Java의 FileNotFoundException

FileNotFoundExample2.java

 // import required classes and packages package javaTpoint.MicrosoftJava; import java.io.*; // it contains all the input and the output streams // create FileNotFoundExceptionExample2 to understand the second point. public class FileNotFoundExceptionExample2 { // main() method start public static void main(String[] args) { try { // creating an instance of the File class to open file File fileObj = new File('Test.txt'); // creating an instance of the PrintWriter class by initiating FileWriter class instance PrintWriter printWriter1 = new PrintWriter(new FileWriter(fileObj), true); // print simple text hello world printWriter1.println('Hello world'); printWriter1.close(); // making Test file read only fileObj.setReadOnly(); // try to write data into Test.txt file PrintWriter printWriter2 = new PrintWriter(new FileWriter('Test.txt'), true); printWriter2.println('Hello World'); printWriter2.close(); } // catching exception thrown by the try block catch(Exception ex) { ex.printStackTrace(); } } } 

산출:

Java의 FileNotFoundException

FileNotFoundException 처리

예외를 처리하려면 try-catch 블록을 사용해야 합니다. try 블록에는 예외를 발생시킬 수 있는 코드 줄을 넣을 것입니다. 예외가 발생할 때마다 catch 블록이 이를 처리합니다. 다른 방법으로 제거할 수 있는 방법이 있습니다. FileNotFountException 그 내용은 다음과 같습니다:

  1. 오류 메시지를 찾으면 해당 파일이나 디렉토리가 없습니다 ; 코드를 다시 확인하고 주어진 파일이 주어진 디렉터리에서 사용 가능한지 여부를 확인하여 해당 예외를 제거할 수 있습니다.
  2. 오류 메시지를 찾으면 접근이 금지되어있다 , 파일의 권한이 우리의 요구 사항에 맞는지 여부를 확인해야 합니다. 권한이 우리의 요구 사항에 맞지 않으면 파일의 권한을 수정해야 합니다.
  3. 을 위한 접근이 금지되어있다 오류 메시지가 나타나면 해당 파일이 다른 프로그램에서 사용 중인지 여부도 확인해야 합니다.
  4. 오류 메시지를 찾으면 지정된 파일은 디렉토리입니다 , 삭제하거나 파일 이름을 변경해야 합니다.

따라서 FileNotFoundExceptionExample1 클래스에서 FileReader 코드를 try-catch 블록에 넣고 지정된 파일 이름이 디렉터리에서 사용 가능한지 확인합니다.

FileNotFoundExample1.java

 // import required classes and packages package javaTpoint.MicrosoftJava; import java.io.*; // it contains all the input and the output streams // create FileNotFoundExceptionExample1 public class FileNotFoundExceptionExample1 { public static void main(String[] args) { // creating an instance of the FileReader class FileReader fileReader; try { fileReader = new FileReader('Test.txt'); // create instance of the BufferedReader and pass the FileReader instance to it. BufferedReader bufferReader = new BufferedReader(fileReader); // declaring an empty string by passing null value String fileData = null; // use while loop to read and print data from buffered reader try { while ((fileData = bufferReader.readLine()) != null) { System.out.println(fileData); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } 

산출:

Java의 FileNotFoundException