logo

Java'da FileNotFoundException

FileNotFoundException mevcut olan başka bir istisna sınıfıdır. java.io paket. İstisna, sistemde bulunmayan bir dosyaya erişmeye çalıştığımızda ortaya çıkar. Bu, kontrol edilen bir istisnadır çünkü derleme zamanında değil, çalışma zamanında meydana gelir ve aşağıdaki kuruculardan biri tarafından oluşturulur:

    Rastgele Erişim Dosyası Dosya Giriş Akışı DosyaÇıktıAkışı
Java'da FileNotFoundException

FileNotFoundException Oluşturucusu

FileNotFoundException sınıfı aşağıdaki iki kurucuya sahiptir:

1.FileNotFoundException()

Bir FileNotFoundException oluşturur ve yapıcıya herhangi bir parametre iletmediğimiz için hata ayrıntı mesajını null olarak ayarlar.

Sözdizimi:

Sözdizimi FileNotFoundException Şöyleki:

 public FileNotFoundException() 

2. FileNotFoundException(Dize dizisi)

Bir FileNotFoundException oluşturur ve hata ayrıntı mesajını ayarlar cadde, bunu yapıcıya aktarıyoruz.

Sözdizimi:

Sözdizimi FileNotFoundException Şöyleki:

 public FileNotFoundException(String str) 

FileNotFoundException Yöntemleri

tarafından sağlanan tüm yöntemleri sağlar. java.lang.Atılabilir ve java.lang.Object sınıflar çünkü bu her iki sınıfın da alt sınıfıdır.

Java.lang.Throwable sınıfının yöntemleri

add Bastırılmış (), fillInStackTrace (), getCause (), getLocalizedMessage (), getMessage (), getStackTrace (), getSuppressed (), initCause (), Yığın İzi yazdır (), Yığın İzi yazdır (), Yığın İzi yazdır (), setStackTrace (), Ve toString ().

Java.lang.Object sınıfının yöntemleri

klon (), eşittir (), Sonuçlandırmak (), getClass (), hash kodu (), bildirmek (), notifyAll (), Ve Beklemek ().

Bu yöntemler hakkında daha fazla bilgi edinmek için aşağıdakileri ziyaret edin:

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

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

FileNotFoundException neden oluşuyor?

Bu hatayı almamızın başlıca iki nedeni vardır. Bu istisnanın alınmasının nedenleri şunlardır:

  1. O dosyaya erişmeye çalıştığımızda sistemde mevcut değil.
  2. Erişilemeyen bir dosyaya erişmeye çalıştığımızda örneğin bir dosya salt okunur işlem için uygunsa ve onu değiştirmeye çalıştığımızda hata verebilir.

Bazı örnekleri ele alalım ve yukarıdaki her iki noktayı da tek tek anlayalım:

DosyaBulunmadıÖrnek1.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(); } } } 

Çıktı:

Java'da FileNotFoundException

DosyaBulunamadıÖrnek2.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(); } } } 

Çıktı:

Java'da FileNotFoundException

FileNotFoundException'ı yönetme

İstisnayı ele almak için try-catch bloğunun kullanılması gerekir. Try bloğuna istisna atabilecek kod satırını koyacağız. Bir istisna oluştuğunda, catch bloğu bunu halledecektir. Kaldırabileceğimiz başka yollar da var FileNotFountException ve bunlar aşağıdaki gibidir:

  1. Hata mesajını bulursak böyle bir dosya veya dizin yok ; Kodu yeniden doğrulayarak ve verilen dosyanın verilen dizinde mevcut olup olmadığını kontrol ederek bu istisnayı kaldırabiliriz.
  2. Hata mesajını bulursak giriş reddedildi , dosyanın izninin ihtiyacımıza uygun olup olmadığını kontrol etmemiz gerekiyor. İzin bizim ihtiyacımıza uygun değilse dosyanın iznini değiştirmemiz gerekir.
  3. İçin giriş reddedildi hata mesajı alıyorsanız, o dosyanın başka bir program tarafından kullanılıp kullanılmadığını da kontrol etmemiz gerekir.
  4. Hata mesajını bulursak belirtilen dosya bir dizindir , onu silmemiz veya dosyanın adını değiştirmemiz gerekiyor.

Yani FileNotFoundExceptionExample1 sınıfında try-catch bloğuna FileReader kodunu yerleştirip verilen dosya adının dizinde bulunmasını sağlıyoruz.

DosyaBulunmadıÖrnek1.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(); } } } 

Çıktı:

Java'da FileNotFoundException