Java FileInputStream Class

Java FileInputStream Class

המחלקה FileInputStream ב-Java משמשת לקריאת נתונים מקובץ בצורה של בתים. זה אידיאלי לקריאת נתונים בינאריים כגון תמונות או קבצי שמע. לקריאת קבצי טקסט עדיף להשתמש FileReader.

  • גישה ישירה: זה קורא ישירות את תוכן הקובץ מהדיסק ללא חציצה
  • עצמאית בפלטפורמה: זה יכול לעבוד על כל מערכת הפעלה

הַצהָרָה

מחלקת FileInputStream מרחיבה את InputStream class כלומר הוא יורש שיטות לקריאת נתוני בתים גולמיים מקבצים.

מחלקה ציבורית FileInputStream מרחיבה את InputStream

דוּגמָה: מחלקה FileInputStream לקריאת נתונים מהקובץ.

Java
   import     java.io.*  ;   public     class   Geeks  {          public     static     void     main  (  String  []     args  ){          // Use try-with-resources to automatically close the      // stream      try     (  FileInputStream     fi      =     new     FileInputStream  (  'file1.txt'  ))     {      // Display file channel information      System  .  out  .  println  (  'Channel: '      +     fi  .  getChannel  ());      // Display file descriptor      System  .  out  .  println  (  'File Descriptor: '      +     fi  .  getFD  ());      // Show available bytes in the stream      System  .  out  .  println  (  'Number of remaining bytes: '      +     fi  .  available  ());      // Skip first few bytes      fi  .  skip  (  4  );      System  .  out  .  println  (  'File Contents:'  );      // Read and print file content      int     ch  ;      while     ((  ch     =     fi  .  read  ())     !=     -  1  )     {      System  .  out  .  print  ((  char  )  ch  );      }      }      catch     (  FileNotFoundException     e  )     {      System  .  out  .  println  (      'File not found: Ensure 'file1.txt' exists in the working directory.'  );      }      catch     (  IOException     e  )     {      System  .  out  .  println  (      'An error occurred while reading the file: '      +     e  .  getMessage  ());      }      }   }   

תְפוּקָה:  

צילום מסךתְפוּקָה

בונים של מחלקה FileInputStream

1. FileInputStream(שם מחרוזת)

יוצר זרם קובץ קלט לקריאה מקובץ בשם שצוין. 

FileInputStream fi = new FileInputStream('example.txt');

2. FileInputStream(קובץ קובץ)

יוצר זרם קובץ קלט לקריאה מאובייקט הקובץ שצוין. 

File f = new File('example.txt');
FileInputStream fi = new FileInputStream(f);

3. FileInputStream(FileDescriptor fdobj)

יוצר זרם קובץ קלט לקריאה מתוך מתאר הקובץ שצוין. 

FileDescriptor fd = FileDescriptor.in;
FileInputStream fi = new FileInputStream(fd); 

צור קובץ בשם file.txt בספריית הפרויקט שלך עם התוכן הבא:

זה הקוד הראשון שלי
זה הקוד השני שלי

Java
   import     java.io.*  ;   public     class   Geeks     {      public     static     void     main  (  String  []     args  )      {      // Use try-with-resources to automatically close the stream      try     (  FileInputStream     fi      =     new     FileInputStream  (  'file1.txt'  ))     {      // Display file channel information      System  .  out  .  println  (  'Channel: '      +     fi  .  getChannel  ());      // Display file descriptor      System  .  out  .  println  (  'File Descriptor: '      +     fi  .  getFD  ());      // Illustrating available method      System  .  out  .  println  (  'Number of remaining bytes: '      +     fi  .  available  ());      // Illustrating skip() method      fi  .  skip  (  4  );      System  .  out  .  println  (  'File Contents:'  );      // Reading characters from FileInputStream      int     ch  ;      while     ((  ch     =     fi  .  read  ())     !=     -  1  )     {      System  .  out  .  print  ((  char  )  ch  );      }      }      catch     (  FileNotFoundException     e  )     {      System  .  out  .  println  (      'File not found: Ensure 'file1.txt' exists in the working directory.'  );      }      catch     (  IOException     e  )     {      System  .  out  .  println  (      'An error occurred while reading the file: '      +     e  .  getMessage  ());      }      }   }   

תְפוּקָה:  

תְפוּקָהתְפוּקָה

שיטות של Java מחלקה FileInputStream

שיטות  בוצעה פעולה 
זָמִין() מחזירה אומדן של מספר הבתים הנותרים שניתן לקרוא (או לדלג עליהם) מזרם קלט זה.
לִסְגוֹר() סוגר את זרם קלט הקובץ הזה ומשחרר את כל משאבי המערכת המשויכים לזרם.
לְסַכֵּם() מבטיח ששיטת הסגירה של זרם קלט קובץ זה נקראת כאשר אין עוד הפניות אליו. 
getChannel() מחזירה את אובייקט FileChannel הייחודי המשויך לזרם קלט קובץ זה. 
getFD() מחזירה את אובייקט FileDescriptor המייצג את החיבור לקובץ בפועל במערכת הקבצים שבה משתמש FileInputStream זה.
לִקְרוֹא() קורא בייט של נתונים מזרם קלט זה
read(byte[] b) קורא עד b.length בתים של נתונים מזרם קלט זה למערך של בתים. 
read(byte[] b int off int len) קורא עד בתים של נתונים מזרם קלט זה למערך של בתים.
לְדַלֵג() מדלג מעל ומסיר n בתים של נתונים מזרם הקלט
צור חידון