Java FileInputStream klase

Java FileInputStream klase

Java FileInputStream klase tiek izmantota, lai nolasītu datus no faila baitu veidā. Tas ir ideāli piemērots bināro datu, piemēram, attēlu vai audio failu, lasīšanai. Teksta failu lasīšanai labāk to izmantot FileReader.

  • Tiešā piekļuve: Tas tieši nolasa faila saturu no diska bez buferizācijas
  • Platformas neatkarīga: Tas var darboties jebkurā operētājsistēmā

Deklarācija

FileInputStream klase paplašina InputStream klase, kas nozīmē, ka tā pārmanto metodes neapstrādātu baitu datu nolasīšanai no failiem.

publiskā klase FileInputStream paplašina InputStream

Piemērs: FileInputStream klase, lai nolasītu datus no faila.

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

Izvade:  

EkrānuzņēmumsIzvade

Konstruktori no FileInputStream klase

1. FileInputStream (virknes nosaukums)

Izveido ievades faila straumi, lai lasītu no faila ar norādīto nosaukumu. 

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

2. FileInputStream (faila fails)

Izveido ievades faila straumi lasīšanai no norādītā faila objekta. 

Fails f = jauns Fails('example.txt');
FileInputStream fi = jauns FileInputStream(f);

3. FileInputStream (FileDescriptor fdobj)

Izveido ievades faila straumi lasīšanai no norādītā faila deskriptora. 

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

Projekta direktorijā izveidojiet failu ar nosaukumu file.txt ar šādu saturu:

šis ir mans pirmais kods
šis ir mans otrais kods

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

Izvade:  

IzvadeIzvade

Java metodes FileInputStream klase

Metodes  Darbība veikta 
pieejams () Atgriež aptuveno atlikušo baitu skaitu, ko var nolasīt (vai izlaist) no šīs ievades straumes.
aizvērt () Aizver šo faila ievades straumi un atbrīvo visus ar straumi saistītos sistēmas resursus.
pabeigt () Nodrošina, lai šīs faila ievades straumes aizvēršanas metode tiktu izsaukta, ja uz to vairs nav atsauces. 
getChannel() Atgriež unikālo FileChannel objektu, kas saistīts ar šo faila ievades straumi. 
getFD() Atgriež FileDescriptor objektu, kas attēlo savienojumu ar faktisko failu failu sistēmā, ko izmanto šī FileInputStream.
lasīt () Nolasa datu baitu no šīs ievades straumes
lasīt (baits[] b) Nolasa līdz b. garuma baitiem datu no šīs ievades straumes baitu masīvā. 
lasīt(baits[] b int off int len) Nolasa līdz len baitiem datu no šīs ievades straumes baitu masīvā.
izlaist () Izlaiž un izmet n baitus datu no ievades straumes
Izveidojiet viktorīnu