logo

Ustvarjanje datoteke z uporabo FileOutputStream

Razred FileOutputStream pripada toku bajtov in hrani podatke v obliki posameznih bajtov. Uporablja se lahko za ustvarjanje besedilnih datotek. Datoteka predstavlja shranjevanje podatkov na drugem pomnilniškem mediju, kot je trdi disk ali CD. Ali je datoteka na voljo ali se lahko ustvari, je odvisno od osnovne platforme. Nekatere platforme zlasti dovoljujejo, da datoteko odpre za pisanje samo en FileOutputStream (ali drugi objekti za pisanje datotek) naenkrat. V takšnih situacijah bodo konstruktorji v tem razredu odpovedali, če je vključena datoteka že odprta. FileOutputStream je namenjen pisanju tokov neobdelanih bajtov, kot so slikovni podatki. Za pisanje tokov znakov razmislite o uporabi FileWriterja. Pomembne metode:
    void close(): Zapre ta izhodni tok datoteke in sprosti vse sistemske vire, povezane s tem tokom. zaščitena praznina finalize(): Očisti povezavo z datoteko in zagotovi, da se pokliče metoda zapiranja tega izhodnega toka datoteke, ko ni več sklicev na ta tok. void write(byte[] b) : Zapiše b.length bajte iz navedene matrike bajtov v ta izhodni tok datoteke. void write(byte[] b int off int len) : Zapiše len bajtov iz navedene matrike bajtov, začenši z odmikom, v ta izhodni tok datoteke. void write(int b): Zapiše podani bajt v izhodni tok te datoteke.
Za ustvarjanje besedilne datoteke, v kateri so shranjeni nekateri znaki (ali besedilo), je treba slediti naslednjim korakom:
    Branje podatkov: 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 Pošlji podatke v 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);
    Branje podatkov iz 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);
    Zapri datoteko:Končno je treba vsako datoteko zapreti po izvedbi vhodnih ali izhodnih operacij v njej, sicer so lahko podatki poškodovani. Datoteko zaprete tako, da zaprete povezane tokove. Na primer fout.close(): bo zaprl FileOutputStream, zato ni mogoče zapisati podatkov v datoteko.
Izvedba: 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); 

Izboljšanje učinkovitosti z uporabo 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.
  • Predpostavimo, da se podatki preberejo s tipkovnice v pomnilnik z uporabo DataInputStream in traja 1 sekunda, da prebere 1 znak v pomnilnik in ta znak v datoteko zapiše FileOutputStream, tako da porabi še 1 sekundo.
  • Tako bo branje in pisanje datoteke trajalo 200 sekund. To je izguba časa. Po drugi strani pa, če se uporabljajo Buffered classed, zagotavljajo medpomnilnik, ki se najprej napolni z znaki iz medpomnilnika, ki jih je mogoče takoj zapisati v datoteko. Medpomnjene razrede je treba uporabljati v povezavi z drugimi tokovnimi razredi.
  • 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
Pomembne metode razreda BufferedOutputStream:
    void flush(): Izprazni ta medpomnilnik izhodni tok. void write(byte[] b int off int len) : Zapiše len bajtov iz navedene matrike bajtov, začenši z odmikom, v ta medpomnilnik izhodni tok. void write(int b): Zapiše podani bajt v ta medpomnilnik izhodni tok.
Izhod:
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 
Sorodni članki:
  • CharacterStream proti ByteStream
  • Datotečni razred v Javi
  • Upravljanje datotek v Javi z uporabo FileWriter in FileReader
Ustvari kviz