In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to achieve buffered input and output stream with Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve buffered input and output stream with Java"!
Buffering is a performance optimization of Icano. The buffered stream adds a memory buffer to the Imax O stream.
BufferedInputStream class and BufferedOutputStream class
The BufferedInputStream class can buffer all subclasses of InputStream for performance optimization.
The flush () method in the BufferedOutputStream class is used to write the bytes of the buffer to the file and clear the cache.
The constructor of the BufferedInputStream class: the constructor introduces BufferedInputStream (FileInputStream fileInputStream); creates a buffered input stream with 32 bytes. BufferedInputStream (FileInputStream fileInputStream, int size); creates a buffered input stream at the specified size. The constructor of the BufferedOutputStream class: the constructor introduces BufferedOutputStream (FileOutputStream fileOutputStream); creates a buffered output stream with 32 bytes. BufferedOutputStream (FileOutputStream fileOutputStream, int size); creates a buffered output stream at the specified size. BufferedInputStream class and BufferedOutputStream class example: import java.io.*; public class Demo4 {public static void main (String [] args) {/ * buffered byte input stream (BufferedInputStream) * Features: improved efficiency * / File file = new File ("C:\\ JAVA_API_1.7 Chinese .chm"); BufferedInputStream bufferedInputStream = null / / create a buffered byte stream FileInputStream fileInputStream= null; long stare = System.currentTimeMillis (); / / get the millisecond value try {fileInputStream=new FileInputStream (file); bufferedInputStream = new BufferedInputStream (fileInputStream) at the beginning of the current stream; / / wrap the file byte stream as a buffered byte stream byte by [] = new byte [1024] / / buffer byte array (this buffer is different from Buffered) while ((bufferedInputStream.read (by))! =-1) {/ / read data using buffered byte stream} long end = System.currentTimeMillis (); / / get the millisecond value System.out.println at the end of the current stream ("milliseconds run:" + (end-stare)) } catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {if (fileInputStream streams) {try {fileInputStream.close ();} catch (IOException e) {e.printStackTrace () }} if (bufferedInputStreamstreams null) {try {bufferedInputStream.close ();} catch (IOException e) {e.printStackTrace () } / * buffered byte output stream (BufferedOutputStream) * Features: improve efficiency * / File file1 = new File ("C:\\ Word.txt"); BufferedOutputStream bufferedOutputStream = null;// create buffered byte output stream FileOutputStream fileOutputStream = null Try {fileOutputStream=new FileOutputStream (file1); bufferedOutputStream=new BufferedOutputStream (fileOutputStream); / / package the file output stream to the buffer byte output stream String str = "Red leaves in the mountains, egrets singing by the ear." ; byte by [] = str.getBytes (); bufferedOutputStream.write (by); / / when using buffered byte output streams, refresh operations should be performed more often to avoid waiting. When data is available, the data will be written to the file bufferedOutputStream.flush (). / / refresh (force buffer data to be written to the file, even if the buffer is not full)} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {if (fileOutputStreamstreams) {try {fileOutputStream.close () } catch (IOException e) {e.printStackTrace ();}} if (bufferedOutputStreamstreams null) {try {bufferedOutputStream.close ();} catch (IOException e) {e.printStackTrace () } BufferedReader class and BufferedWriter class
The BufferedReader class and the BufferedWriter class inherit the Reader class and the Writer class, respectively, which also have internal buffering mechanisms and input / output in behavioral units.
Common methods of the BufferedReader class:
Common methods of the BufferedWriter class:
Examples of BufferedReader class and BufferedWriter class: import java.io.*; public class Demo6 {public static void main (String [] args) {File file = new File ("C:\\ Word.txt"); / * * File buffered character output stream (BufferedWriter) * / FileWriter fileWriter = null;// create file character output stream BufferedWriter bufferedWriter = null / / create a file buffer character output stream try {fileWriter = new FileWriter (file); bufferedWriter = new BufferedWriter (fileWriter); / / package the file character output stream into a file buffer character output stream String str1 = "Shenli"; String str2 = "Ayanhua"; bufferedWriter.write (str1); / / the first line of data bufferedWriter.newLine () / / create a new row bufferedWriter.write (str2); / / the second row of data} catch (IOException e) {e.printStackTrace ();} finally {/ / Note: the order in which the stream is closed, created first and then closed. If {try {bufferedWriter.close ();} catch (IOException e) {e.printStackTrace ();}} if {try {fileWriter.close () } catch (IOException e) {e.printStackTrace ();} / * File buffered character input stream (BufferedReader) * / FileReader fileReader = null; BufferedReader bufferedReader = null; try {fileReader = new FileReader (file) BufferedReader = new BufferedReader (fileReader); / / package the file character input stream as a file buffer character input stream String tmp = null;// temporary variable int I = 1 bang / counter while ((tmp = bufferedReader.readLine ())! = null) {/ / read the contents of the file in a loop System.out.println ("line" + I + ":" + tmp) Catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {try {bufferedReader.close () } catch (IOException e) {e.printStackTrace ();}} if (fileReaderships null) {try {fileReader.close ();} catch (IOException e) {e.printStackTrace () At this point, I believe you have a deeper understanding of "how to achieve buffered input and output streams in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.