English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
في هذا الدرس، سنتعلم باستخدام أمثلة عن FileInputStream في Java ومعطياته.
يمكن استخدام class FileInputStream في مكتبة java.io لقراءة البيانات من الملف (بأبعاد البايتات).
أنه يرث من class InputStream التجريدي.
قبل تعلم FileInputStream، تأكد من أنك تعرفJava File (ملف).
لإنشاء تدفق إدخال ملف، يجب علينا أولاً استيراد مكتبة java.io.FileInputStream. بعد استيراد المكتبة، يمكننا استخدام Java لإنشاء تدفق إدخال ملف.
1. باستخدام مسار الملف
FileInputStream input = new FileInputStream(stringPath);
في هذا المكان، قمنا بإنشاء تدفق إدخال سيره إلى مسار الملف المحدد.
2. باستخدام ملف الهدف
FileInputStream input = new FileInputStream(File fileObject);
في هذا المكان، قمنا بإنشاء تدفق إدخال سيره إلى ملف محدد من قبل fileObject.
يقدم class FileInputStream تنفيذًا لمختلف الطرق التي تظهر في class InputStream.
read() - قراءة بيت واحد من الملف
read(byte[] array) - قراءة بيتات من الملف وتخزينها في المجموعة المحددة
read(byte[] array, int start, int length) - قراءة عدد من البايتات يساوي length من الملف، وتخزينها في الموقع start من المجموعة المحددة
افترض أن لدينا ملفًا يُدعىinput.txtالملف الذي يحتوي على ما يلي.
هذا سطر نصي داخل الملف.
لنحاول استخدام FileInputStream لقراءة هذا الملف.
import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { FileInputStream input = new FileInputStream("input.txt"); System.out.println("معلومات الملف: "); // قراءة البايت الأول int i = input.read(); while(i != -1) { System.out.print((char)i); // من قراءة التالية من الملف i = input.read(); } input.close(); } catch(Exception e) {}} e.getStackTrace(); } } }
Output Result
بيانات الملف: هذا سطر نصي داخل الملف.
في هذا المثال، قمنا بإنشاء تدفق إدخال ملف يُدعى input. تدفق الإدخال معinput.txtرابط الملف
FileInputStream input = new FileInputStream("input.txt");
للقراءة من الملف، استخدمنا طريقة read() في الدورات بينما
للحصول على عدد البايتات المتاحة، يمكنك استخدام طريقة available(). على سبيل المثال،
import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { // افترض أن ملف input.txt يحتوي على النص التالي // هذا سطر نصي في الملف FileInputStream input = new FileInputStream("input.txt"); // العودة لعدد البايتات المتاحة System.out.println("عدد البايتات المتاحة عند البداية: " + input.available()); // قراءة 3 بايت من الملف input.read(); input.read(); input.read(); // العودة لعدد البايتات المتاحة System.out.println("عدد البايتات المتاحة عند النهاية: " + input.available()); input.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output Result
عدد البايتات المتاحة عند البداية: 39 عدد البايتات المتاحة عند النهاية: 36
في هذا المثال،
أولاً، استخدمنا طريقة available() لفحص عدد البايتات المتاحة في تدفق الإدخال إلى الملف.
ثم، استخدمنا طريقة read() ثلاث مرات لقراءة 3 بايت من تدفق الإدخال إلى الملف.
الآن، بعد قراءة البايت، قمنا مرة أخرى بفحص عدد البايتات المتاحة. هذه المرة، قلت عدد البايتات المتاحة بـ 3.
لإهمال وتخطي عدد معين من البايتات، يمكنك استخدام طريقة skip(). على سبيل المثال
import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { // افترض أن ملف input.txt يحتوي على النص التالي // هذا سطر نصي في الملف FileInputStream input = new FileInputStream("input.txt"); // تخطي 5 بايت input.skip(5); System.out.println("تخطي 5 بايت من التدفق الإدخال:"); // قراءة البايت الأول int i = input.read(); while (i != -1) { System.out.print((char) i); // من قراءة التالية من الملف i = input.read(); } // قفل() طريقة input.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output Result
Input stream skip 5 bytes: is a line of text inside the file.
In the above example, we used the skip() method to skip 5 bytes of data from the file input stream. Therefore, the text represented by the following bytes will not be read from the input stream:“This”bytes.
To close the file input stream, you can use the close() method. Once the close() method is called, we cannot use the input stream to read data.
In all the above examples, we have used the close() method to close the file input stream.
Method | Content Description |
---|---|
finalize() | Ensure that the close() method is called |
getChannel() | Return the object associated with the FileChannel and the input stream |
getFD() | Return the file descriptor associated with the input stream |
mark() | Mark the position of the data read from the input stream |
reset() | Return the control to the point in the input stream that was marked |