Razred Java FileInputStream

Razred Java FileInputStream

Razred FileInputStream v Javi se uporablja za branje podatkov iz datoteke v obliki bajtov. Idealen je za branje binarnih podatkov, kot so slike ali zvočne datoteke. Za branje besedilnih datotek je bolje uporabiti FileReader.

  • Neposreden dostop: Neposredno prebere vsebino datoteke z diska brez medpomnjenja
  • Neodvisno od platforme: Deluje lahko na katerem koli operacijskem sistemu

Izjava

Razred FileInputStream razširja InputStream kar pomeni, da podeduje metode za branje surovih bajtnih podatkov iz datotek.

javni razred FileInputStream razširja InputStream

primer: Razred FileInputStream za branje podatkov iz datoteke.

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

Izhod:  

Posnetek zaslonaIzhod

Konstruktorji od Razred FileInputStream

1. FileInputStream(ime niza)

Ustvari tok vhodne datoteke za branje iz datoteke s podanim imenom. 

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

2. FileInputStream(Datoteka datoteke)

Ustvari vhodni datotečni tok za branje iz podanega predmeta File. 

Datoteka f = nova datoteka ('example.txt');
FileInputStream fi = nov FileInputStream(f);

3. FileInputStream(FileDescriptor fdobj)

Ustvari tok vhodne datoteke za branje iz podanega deskriptorja datoteke. 

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

V imeniku projekta ustvarite datoteko z imenom file.txt z naslednjo vsebino:

to je moja prva koda
to je moja druga koda

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

Izhod:  

IzhodIzhod

Metode Jave Razred FileInputStream

Metode  Dejanje izvedeno 
na voljo() Vrne oceno števila preostalih bajtov, ki jih je mogoče prebrati (ali preskočiti) iz tega vhodnega toka.
zapri() Zapre ta vhodni tok datoteke in sprosti vse sistemske vire, povezane s tokom.
dokončaj() Zagotavlja, da se metoda zapiranja tega vhodnega toka datoteke pokliče, ko ni več sklicev nanjo. 
getChannel() Vrne edinstven predmet FileChannel, povezan s tem vhodnim tokom datoteke. 
getFD() Vrne predmet FileDescriptor, ki predstavlja povezavo z dejansko datoteko v datotečnem sistemu, ki ga uporablja ta FileInputStream.
preberi() Prebere bajt podatkov iz tega vhodnega toka
branje (bajt [] b) Prebere do b.length bajtov podatkov iz tega vhodnega toka v niz bajtov. 
read(byte[] b int off int len) Prebere do len bajtov podatkov iz tega vhodnega toka v polje bajtov.
preskoči() Preskoči in zavrže n bajtov podatkov iz vhodnega toka
Ustvari kviz