English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
في هذا الدرس، سنتعلم عن Java BufferedInputStream ومетодاته من خلال مثال.
يستخدم فئة BufferedInputStream في حزمة java.io مع تدفقات الإدخال الأخرى لقراءة البيانات بشكل أكثر فعالية (بشكل بايتات).
انه ورث من فئة InputStream التجريدية.
يحافظ BufferedInputStream على8192 بايتمنطقة التخزين الداخلية.
أثناء تنفيذ عمليات القراءة في BufferedInputStream، سيتم قراءة جزء من البايتات من القرص وستتم تخزينها في منطقة التخزين الداخلية. ثم سيتم قراءة البايتات الواحدة تلو الأخرى من منطقة التخزين الداخلية.
لذلك، قلل من عدد الاتصالات مع القرص. هذا هو السبب في أن قراءة البيانات باستخدام BufferedInputStream أسرع.
لإنشاء BufferedInputStream، يجب علينا أولاً استيراد حزمة java.io.BufferedInputStream. بعد استيراد الحزمة، يمكننا إنشاء تدفق الإدخال هنا.
//إنشاء FileInputStream FileInputStream file = new FileInputStream(String path); //创建一个BufferedInputStream BufferedInputStream buffer = new BufferedInputStream(file);
في المثال السابق، قمنا بإنشاء buffer يُدعى BufferdInputStream باستخدام FileInputStream يُدعى file
في هذا السياق، يكون حجم مساحة التخزين الداخليه الافتراضي 8192 بايت. ولكن يمكن أيضًا تحديد حجم مساحة التخزين الداخليه
//إنشاء BufferedInputStream يحتوي على مساحة داخليه مسبقة التحديد من الحجم المحدد BufferedInputStream buffer = new BufferedInputStream(file, int size);
سيكون buffer مفيدًا لقراءة البايت من الملف بشكل أسرع
يقدم BufferedInputStream تنفيذًا للطرق المختلفة المتاحة في类 InputStream
read() - قراءة بايت واحد من تدفق الإدخال
read(byte[] arr) - قراءة البايت وتخزينه في المجموعة المحددة من البايت
read(byte[] arr, int start, int length) - قراءة عدد البايت يساوي length من التدفق وتخزينه في المجموعة المحددة من البايت بدءًا من الموقع start
افترض أن لدينا ملفًا يُدعىinput.txtالذي يحتوي على ما يلي.
هذا سطر نصي داخل الملف.
لنحاول استخدام BufferedInputStream لقراءة الملف.
import java.io.BufferedInputStream; import java.io.FileInputStream; class Main { public static void main(String[] args) { try { //إنشاء FileInputStream FileInputStream file = new FileInputStream("input.txt"); //创建一个BufferedInputStream BufferedInputStream input = new BufferedInputStream(file); //قراءة البايت الأول من الملف int i = input.read(); while (i != -1) { System.out.print((char) i); //قراءة البايت التالي من الملف i = input.read(); } input.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output Result
هذا سطر نصي داخل الملف.
في هذا المثال، قمنا بإنشاء تدفق إدخال م缓冲 وFileInputStream. ربط تدفق الإدخال بملف input.txt.
FileInputStream file = new FileInputStream("input.txt"); BufferedInputStream buffer = new BufferedInputStream(file);
在这里,我们使用了read()方法从缓冲读取器的内部缓冲区读取字节数组。
要获取输入流中可用字节的数量,我们可以使用available()方法。例如,
import java.io.FileInputStream; import java.io.BufferedInputStream; public class Main { public static void main(String args[]) { try { //假设input.txt文件包含以下文本 //这是文件中的一行文本。 FileInputStream file = new FileInputStream("input.txt"); //创建一个BufferedInputStream BufferedInputStream buffer = new BufferedInputStream(file); //返回可用的字节数 System.out.println("开始时可用的字节数: " + buffer.available()); //从文件中读取字节 buffer.read(); buffer.read(); buffer.read(); //返回可用的字节数 System.out.println("结束时的可用字节数: " + buffer.available()); buffer.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output Result
开始时可用的字节数: 39 结束时的可用字节数: 36
在以上示例中,
我们首先使用available()方法检查输入流中可用字节的数量。
然后,我们使用read()方法3次从输入流中读取3个字节。
现在,在读取字节之后,我们再次检查了可用字节。这一次可用字节减少了3。
要丢弃和跳过指定的字节数,可以使用skip()方法。例如
import java.io.FileInputStream; import java.io.BufferedInputStream; public class Main { public static void main(String args[]) { try { //假设input.txt文件包含以下文本 //这是文件中的一行文本。 FileInputStream file = new FileInputStream("input.txt"); //创建一个BufferedInputStream BufferedInputStream buffer = new BufferedInputStream(file); //跳过5个字节 buffer.skip(5); System.out.println("跳过5个字节后的输入流:"); //从输入流中读取第一个字节 int i = buffer.read(); while (i != -1) { System.out.print((char) i); //Read the next byte from the input stream i = buffer.read(); } //Close the input stream buffer.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output Result
Input stream after skipping 5 bytes: is a line of text inside the file.
In the above example, we have used the skip() method to skip 5 bytes from the file input stream. Therefore, skipping the bytes 'T', 'h', 'i', 's', and ' ' from the input stream.
To close the buffered input stream, we can use the close() method. Once the close() method is called, we will not be able to read data from the input stream.
Method | Content Description |
---|---|
mark() | Mark the position of the read data in the input stream |
reset() | Return the control to the point in the input stream that was marked |