logo

Faila izveide, izmantojot FileOutputStream

FileOutputStream klase pieder baitu straumei un saglabā datus atsevišķu baitu veidā. To var izmantot, lai izveidotu teksta failus. Fails apzīmē datu glabāšanu otrā datu nesējā, piemēram, cietajā diskā vai kompaktdiskā. Tas, vai fails ir pieejams vai var tikt izveidots, ir atkarīgs no pamatā esošās platformas. Dažas platformas jo īpaši ļauj failu atvērt rakstīšanai tikai vienam FileOutputStream (vai citiem failu rakstīšanas objektiem) vienlaikus. Šādās situācijās šīs klases konstruktori neizdosies, ja iesaistītais fails jau ir atvērts. FileOutputStream ir paredzēts neapstrādātu baitu, piemēram, attēla datu, straumju rakstīšanai. Lai rakstītu rakstzīmju straumes, apsveriet iespēju izmantot FileWriter. Svarīgas metodes:
    tukša aizvēršana () : Aizver šo faila izvades straumi un atbrīvo visus ar šo straumi saistītos sistēmas resursus. aizsargāta spēkā esamība finalize() : Attīra savienojumu ar failu un nodrošina, ka šī faila izvades straumes aizvēršanas metode tiek izsaukta, ja uz šo straumi vairs nav atsauces. nederīgs rakstīšana (baits[] b) : Šajā faila izvades straumē ieraksta b.length baitus no norādītā baitu masīva. void write(baits[] b int off int len) : Šajā faila izvades straumē ieraksta len baitus no norādītā baitu masīva, sākot ar nobīdi. nederīgs raksts (b int): Ieraksta norādīto baitu šajā faila izvades straumē.
Lai izveidotu teksta failu, kurā ir saglabātas dažas rakstzīmes (vai teksts), ir jāveic šādas darbības:
    Datu nolasīšana: First of all data should be read from the keyboard. For this purpose associate the keyboard to some input stream class. The code for using DataInputSream class for reading data from the keyboard is as:
    DataInputStream dis =new DataInputStream(System.in);
    Here System.in represent the keyboard which is linked with DataInputStream object Sūtīt datus uz OutputStream: Now associate a file where the data is to be stored to some output stream. For this take the help of FileOutputStream which can send data to the file. Attaching the file.txt to FileOutputStream can be done as:
    FileOutputStream fout=new FileOutputStream(file.txt);
    Datu nolasīšana no DataInputStream: The next step is to read data from DataInputStream and write it into FileOutputStream . It means read data from dis object and write it into fout object as shown here:
    ch=(char)dis.read(); fout.write(ch);
    Aizveriet failu:Visbeidzot, jebkurš fails ir jāaizver pēc ievades vai izvades operāciju veikšanas, pretējā gadījumā faila dati var tikt bojāti. Faila aizvēršana tiek veikta, aizverot saistītās straumes. Piemēram, fout.close(): aizvērs FileOutputStream, tāpēc failā nevar ierakstīt datus.
Īstenošana: Java
//Java program to demonstrate creating a text file using FileOutputStream import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; class Create_File {  public static void main(String[] args) throws IOException   {  //attach keyboard to DataInputStream  DataInputStream dis=new DataInputStream(System.in);  // attach file to FileOutputStream  FileOutputStream fout=new FileOutputStream('file.txt');  //attach FileOutputStream to BufferedOutputStream  BufferedOutputStream bout=new BufferedOutputStream(fout1024);  System.out.println('Enter text (@ at the end):');  char ch;  //read characters from dis into ch. Then write them into bout.  //repeat this as long as the read character is not @  while((ch=(char)dis.read())!='@')  {  bout.write(ch);  }  //close the file  bout.close();  } } 
If the Program is executed again the old data of file.txt will be lost and any recent data is only stored in the file. If we don’t want to lose the previous data of the file and just append the new data to the end of already existing data and this can be done by writing true along with file name.
FileOutputStream fout=new FileOutputStream(file.txttrue); 

Efektivitātes uzlabošana, izmantojot BufferedOutputStream

Normally whenever we write data to a file using FileOutputStream as:
fout.write(ch);
Here the FileOutputStream is invoked to write the characters into the file. Let us estimate the time it takes to read 100 characters from the keyboard and write all of them into a file.
  • Pieņemsim, ka dati tiek nolasīti no tastatūras atmiņā, izmantojot DataInputStream, un ir nepieciešama 1 sekunde, lai nolasītu atmiņā 1 rakstzīmi, un šī rakstzīme tiek ierakstīta failā, izmantojot FileOutputStream, pavadot vēl 1 s.
  • Tātad faila lasīšanai un rakstīšanai būs nepieciešamas 200 sekundes. Tas patērē daudz laika. No otras puses, ja tiek izmantotas bufera klases, tās nodrošina buferi, kas vispirms tiek aizpildīts ar bufera rakstzīmēm, kuras var uzreiz ierakstīt failā. Buferētās klases ir jāizmanto saistībā ar citām straumes klasēm.
  • First the DataInputStream reads data from the keyboard by spending 1 sec for each character. This character is written into the buffer. Thus to read 100 characters into a buffer it will take 100 second time. Now FileOutputStream will write the entire buffer in a single step. So reading and writing 100 characters took 101 sec only. In the same way reading classes are used for improving the speed of reading operation.  Attaching FileOutputStream to BufferedOutputStream as:
    BufferedOutputStream bout=new BufferedOutputStream(fout1024);
    Here the buffer size is declared as 1024 bytes. If the buffer size is not specified then a default size of 512 bytes is used
Svarīgas BufferedOutputStream klases metodes:
    void flush() : Izskalo šo buferizēto izvades straumi. void write(baits[] b int off int len) : Šajā buferizētajā izvades straumē ieraksta len baitus no norādītā baitu masīva, sākot ar nobīdi. nederīgs raksts (b int): Ieraksta norādīto baitu šajā buferizētajā izvades straumē.
Izvade:
C:> javac Create_File.java C:> java Create_File Enter text (@ at the end): This is a program to create a file @ C:/> type file.txt This is a program to create a file 
Saistītie raksti:
  • CharacterStream vs ByteStream
  • Failu klase Java
  • Failu apstrāde Java, izmantojot FileWriter un FileReader
Izveidojiet viktorīnu