logo

إنشاء ملف باستخدام FileOutputStream

تنتمي فئة FileOutputStream إلى دفق البايت وتقوم بتخزين البيانات في شكل بايتات فردية. يمكن استخدامه لإنشاء ملفات نصية. يمثل الملف تخزين البيانات على وسائط تخزين ثانية مثل القرص الصلب أو القرص المضغوط. يعتمد ما إذا كان الملف متاحًا أو يمكن إنشاؤه أم لا على النظام الأساسي الأساسي. تسمح بعض الأنظمة الأساسية على وجه الخصوص بفتح الملف للكتابة بواسطة FileOutputStream واحد فقط (أو كائنات أخرى لكتابة الملفات) في المرة الواحدة. في مثل هذه الحالات، سيفشل المنشئون في هذه الفئة إذا كان الملف المعني مفتوحًا بالفعل. تم تصميم FileOutputStream لكتابة تدفقات البايتات الأولية مثل بيانات الصورة. لكتابة تدفقات من الأحرف، فكر في استخدام FileWriter. طرق مهمة:
    إغلاق باطل () : يغلق دفق إخراج الملف هذا ويحرر أي موارد نظام مرتبطة بهذا الدفق. الانتهاء من الفراغ المحمي (): ينظف الاتصال بالملف ويضمن استدعاء طريقة الإغلاق لدفق إخراج الملف هذا عندما لا يكون هناك المزيد من المراجع لهذا الدفق. الكتابة باطلة (بايت [] ب): يكتب بايتات b.length من صفيف البايت المحدد إلى دفق إخراج الملف هذا. الكتابة باطلة (بايت [] ب int off int len): يكتب بايتات لين من صفيف البايت المحدد بدءًا من الإزاحة إلى دفق إخراج الملف هذا. الكتابة الفارغة (int ب): يكتب البايت المحدد لدفق إخراج الملف هذا.
يجب اتباع الخطوات التالية لإنشاء ملف نصي يقوم بتخزين بعض الأحرف (أو النص):
    قراءة البيانات: 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 إرسال البيانات إلى 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);
    قراءة البيانات من 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);
    أغلق الملف:وأخيرًا، يجب إغلاق أي ملف بعد إجراء عمليات الإدخال أو الإخراج عليه وإلا قد تتلف بياناته. يتم إغلاق الملف عن طريق إغلاق التدفقات المرتبطة به. على سبيل المثال fout.Close(): سيغلق FileOutputStream وبالتالي لا توجد طريقة لكتابة البيانات في الملف.
تطبيق: 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); 

تحسين الكفاءة باستخدام 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.
  • لنفترض أنه تتم قراءة البيانات من لوحة المفاتيح إلى الذاكرة باستخدام DataInputStream ويستغرق الأمر ثانية واحدة لقراءة حرف واحد في الذاكرة ويتم كتابة هذا الحرف في الملف بواسطة FileOutputStream عن طريق قضاء ثانية واحدة أخرى.
  • لذا فإن قراءة وكتابة ملف سيستغرق 200 ثانية. وهذا يضيع الكثير من الوقت. من ناحية أخرى، إذا تم استخدام التصنيف المخزن مؤقتًا، فإنه يوفر مخزنًا مؤقتًا يتم ملؤه أولاً بأحرف من المخزن المؤقت والتي يمكن كتابتها مرة واحدة في الملف. يجب استخدام الفئات المخزنة مؤقتًا فيما يتعلق بفئات الدفق الأخرى.
  • 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
طرق مهمة لفئة BufferedOutputStream:
    تدفق الفراغ () : يمسح دفق الإخراج المخزن مؤقتًا. الكتابة باطلة (بايت [] ب int off int len): يكتب بايتات لين من صفيف البايت المحدد بدءًا من الإزاحة إلى دفق الإخراج المخزن مؤقتًا. الكتابة الفارغة (int ب): يكتب البايت المحدد إلى دفق الإخراج المخزن مؤقتًا.
الإخراج:
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 
مقالات ذات صلة:
  • CharacterStream مقابل ByteStream
  • فئة الملف في جافا
  • معالجة الملفات في Java باستخدام FileWriter وFileReader
إنشاء اختبار