logo

FileNotFoundException ב-Java

FileNotFoundException הוא מעמד חריג נוסף זמין ב- java.io חֲבִילָה. החריג מתרחש כאשר אנו מנסים לגשת לאותו קובץ שאינו זמין במערכת. זהו חריג מסומן מכיוון שהוא מתרחש בזמן ריצה, לא בזמן הידור, והוא נזרק על ידי אחד מהבנאים הבאים:

    RandomAccessFile FileInputStream FileOutputStream
FileNotFoundException ב-Java

FileNotFoundException Constructor

למחלקה FileNotFoundException יש את שני הבנאים הבאים:

1. FileNotFoundException()

זה בונה FileNotFoundException ומגדיר את הודעת פרטי השגיאה null כי לא העברנו שום פרמטר לבנאי.

תחביר:

התחביר של ה FileNotFoundException הוא כדלקמן:

 public FileNotFoundException() 

2. FileNotFoundException(String str)

זה בונה FileNotFoundException ומגדיר את הודעת פירוט השגיאה str, שאנו מעבירים לבנאי.

תחביר:

התחביר של ה FileNotFoundException הוא כדלקמן:

 public FileNotFoundException(String str) 

שיטות FileNotFoundException

הוא מספק את כל השיטות הניתנות על ידי java.lang.ניתן לזרוק וה java.lang.Object מחלקות מכיוון שהיא תת מחלקה של שתי המחלקות הללו.

שיטות של Java.lang.Throwable class

addSuppressed (), fillInStackTrace (), getCause (), getLocalizedMessage (), getMessage (), getStackTrace (), getSuppressed (), initCause (), printStackTrace (), printStackTrace (), printStackTrace (), setStackTrace (), ו toString ().

שיטות של Java.lang.Object class

שיבוט (), שווים (), לְסַכֵּם (), getClass (), hashcode (), לְהוֹדִיעַ (), הודע לכל (), ו לַחֲכוֹת ().

למידע נוסף על שיטות אלה, בקר בכתובת הבאה:

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

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

מדוע מתרחשת FileNotFoundException?

יש בעיקר שתי סיבות שבגללן אנו מקבלים את השגיאה הזו. הסיבות לקבלת חריג זה הן כדלקמן:

  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(); } } } 

תְפוּקָה:

FileNotFoundException ב-Java

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(); } } } 

תְפוּקָה:

FileNotFoundException ב-Java

טיפול ב-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(); } } } 

תְפוּקָה:

FileNotFoundException ב-Java