logo

Fájl létrehozása a FileOutputStream segítségével

A FileOutputStream osztály a byte folyamhoz tartozik, és az adatokat egyedi bájtok formájában tárolja. Használható szöveges fájlok létrehozására. A fájl az adatok tárolását jelenti egy második adathordozón, például merevlemezen vagy CD-n. Az, hogy egy fájl elérhető-e vagy sem, az alapul szolgáló platformtól függ. Egyes platformok különösen lehetővé teszik, hogy egy fájlt egyszerre csak egy FileOutputStream (vagy más fájlíró objektum) nyisson meg írásra. Ilyen helyzetekben az osztály konstruktorai meghibásodnak, ha az érintett fájl már meg van nyitva. A FileOutputStream nyers bájtok, például képadatok adatfolyamainak írására szolgál. Karakterfolyamok írásához fontolja meg a FileWriter használatát. Fontos módszerek:
    void close() : Bezárja ezt a fájlkimeneti adatfolyamot, és felszabadítja az adatfolyamhoz kapcsolódó összes rendszererőforrást. védett void finalize() : Megtisztítja a kapcsolatot a fájllal, és biztosítja, hogy a fájl kimeneti adatfolyamának bezárási metódusa akkor kerüljön meghívásra, ha már nincs hivatkozás erre az adatfolyamra. void write(byte[] b) : B.length bájtokat ír a megadott bájttömbből ebbe a fájlkimeneti adatfolyamba. void write(byte[] b int off int len) : Len bájtokat ír a megadott bájttömbből az eltolástól kezdődően ebbe a fájlkimeneti adatfolyamba. érvénytelen írás (b int): A megadott bájtot ebbe a fájlkimeneti adatfolyamba írja.
Néhány karaktert (vagy szöveget) tartalmazó szövegfájl létrehozásához a következő lépéseket kell követni:
    Adatok olvasása: 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 Adatok küldése az OutputStreamnek: 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);
    Adatok olvasása a DataInputStreamből: 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);
    Zárja be a fájlt:Végül minden fájlt be kell zárni, miután beviteli vagy kimeneti műveleteket hajt végre rajta, különben a fájl adatai megsérülhetnek. A fájl bezárása a kapcsolódó folyamok bezárásával történik. Például a fout.close(): bezárja a FileOutputStream-et, így nem lehet adatokat írni a fájlba.
Végrehajtás: 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); 

A hatékonyság javítása a BufferedOutputStream használatával

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.
  • Tételezzük fel, hogy az adatokat a billentyűzetről a DataInputStream segítségével olvassa be a memóriába, és 1 másodpercbe telik 1 karakter beolvasása a memóriába, és ezt a karaktert a FileOutputStream további 1 másodperc elköltésével írja be a fájlba.
  • Tehát egy fájl olvasása és írása 200 másodpercet vesz igénybe. Ez sok időt veszít. Másrészt, ha a pufferelt osztályozást használjuk, akkor egy puffert biztosítanak, amelyet először a pufferből származó karakterekkel töltenek meg, amelyeket azonnal be lehet írni a fájlba. A pufferelt osztályokat más adatfolyam-osztályokkal kapcsolatban kell használni.
  • 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
A BufferedOutputStream osztály fontos módszerei:
    void flush() : Kiüríti ezt a pufferelt kimeneti adatfolyamot. void write(byte[] b int off int len) : Len bájtokat ír a megadott bájttömbből az eltolástól kezdve ehhez a pufferelt kimeneti adatfolyamhoz. érvénytelen írás (b int): A megadott bájtot ebbe a pufferelt kimeneti adatfolyamba írja.
Kimenet:
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 
Kapcsolódó cikkek:
  • CharacterStream vs ByteStream
  • Fájlosztály Java nyelven
  • Fájlkezelés Java nyelven FileWriter és FileReader segítségével
Kvíz létrehozása