Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Packaging flow in Java IO

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of packaging flow in Java IO, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

According to the function, it is divided into node flow and wrapper flow (processing flow)

Node flow: you can read and write data from or to a specific place (node). Such as FileReader.

Processing flow: it is the connection and encapsulation of an existing stream, and the data is read and written through the function call of the encapsulated stream. Such as BufferedReader. The constructor of the processing flow always takes another stream object as a parameter. A stream object is wrapped multiple times by other streams and is called a link to the stream.

1. The character input and output streams and byte input and output streams mentioned earlier are all byte streams. So what is packaging flow?

① and wrapper flow hide the differences of the underlying node flow, and provide more convenient input / output functions, so let's only care about the operation of this advanced stream.

②, wraps the node flow with the wrapper flow, the program operates the wrapper flow directly, but the underlying layer is still the node stream and IO device operation.

③, when closing the packaging stream, you only need to close the packaging stream

2. Buffer flow

Buffer stream: a wrapper stream designed to act as a cache and speed up reading and writing data.

Byte buffer streams: BufferedInputStream, BufferedOutputStream

Character buffer streams: BufferedReader, BufferedWriter

Case playback: when we put the character input / output stream and byte input / output stream, the read operation usually defines a byte or character array and stores the read / write data into this array first. and then fetch the data in the array. This is much faster than reading / writing data one by one, and this is where the buffer stream comes from. It's just that there is an array defined in the buffer stream to store the data we read / write, and when the internally defined array is full (note: when we operate, a small array is still defined externally, and the decimal array is put into the internal array). The next step will be done.

Here are the operations that do not use buffered flows:

/ / 1. Create a target object, and the input stream indicates that the data of that file is saved to the program. Do not write the drive letter. By default, the file is saved in the root directory of the project / / a.txt: AAaBCDEF File target = new File ("io" + File.separator+ "a.txt"); / / 2. Create the input stream object InputStream in = new FileInputStream (target). / / 3. Specific IO operation (reading the data in the a.txt file into the program) / * Note: read the data in the file and return-1 * int read (): read a byte when there is no data in the end. Return read bytes * int read (byte [] b): read multiple bytes and save them to array b, store them from the position where array b has index 0, return several bytes read * int read (byte [] b byte int off,int len): read multiple bytes and store them in array b, starting at the position where array b has index 0 Length is len bytes * / int read (): reads a byte and returns the read byte int data1 = in.read () / / get the first byte of the data in the a.txt file System.out.println ((char) data1); / / A / / int read (byte [] b): read multiple bytes and save them in array b byte [] buffer = new byte [10]; / / here we define a byte array of length 10 to store the read data in.read (buffer) / / get the first 10 bytes in the a.txt file and store them in the buffer array System.out.println (Arrays.toString (buffer)); / / [65, 97, 66, 67, 68, 69, 70, 0, 0, 0] System.out.println (new String (buffer)) / / AaBCDEF [] / / int read (byte [] b off,int len): read multiple bytes and store them in array b, starting from index off to len in.read (buffer, 0,3); System.out.println (Arrays.toString (buffer)); / / [65,97,66,0,0,0,0,0,0,0,0] System.out.println (new String (buffer)) / / AaB [] / 4. Close the stream resource in.close ()

When we look at the underlying JDK source code of the buffer stream, we can see that such a cache array is defined in the program, with a size of 8192.

BufferedInputStream:

BufferedOutputStream:

/ / Byte buffered input stream BufferedInputStream bis = new BufferedInputStream (new FileInputStream ("io" + File.separator+ "a.txt")); / / defines a byte array to store data byte [] buffer = new byte [1024]; int len =-1 / / define an integer indicating the number of bytes read while ((len=bis.read (buffer))! =-1) {System.out.println (new String (buffer,0,len));} / close stream resource bis.close () / / Byte buffered output stream BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream ("io" + File.separator+ "a.txt"); bos.write ("ABCD" .getBytes ()); bos.close (); / / character buffered input stream BufferedReader br = new BufferedReader (new FileReader ("io" + File.separator+ "a.txt")) Char [] buffer = new char [10]; int len=-1; while ((len=br.read (buffer))! =-1) {System.out.println (new String (buffer,0,len));} br.close (); / / character buffered output stream BufferedWriter bw = new BufferedWriter (new FileWriter ("io" + File.separator+ "a.txt")) Bw.write ("ABCD"); bw.close (); 3. Conversion stream: converting a byte stream to a character stream

InputStreamReader: convert byte input stream to character input stream

OutputStreamWriter: convert byte output stream to character output stream

Copy files with a conversion stream:

/ * copy a.txt file into b.txt * / 1, create source and destination File srcFile = new File ("io" + File.separator+ "a.txt"); File descFile = new File ("io" + File.separator+ "b.txt"); / / 2. Create byte input / output stream object InputStream in = new FileInputStream (srcFile) OutputStream out = new FileOutputStream (descFile); / / 3, create conversion input and output objects Reader rd = new InputStreamReader (in); Writer wt = new OutputStreamWriter (out); / / 3, read and write operation char [] buffer = new char [10]; / / create a character array with a capacity of 10, storing the data that has been read int len =-1 / / indicates how many characters have been read. If-1, the end of the file while ((len=rd.read (buffer))! =-1) {wt.write (buffer, 0, len) {wt.write (0, len);} / / 4. Close the stream resource rd.close (); wt.close (); 4. Memory stream (array stream):

Temporarily store the data in the array, that is, in memory. So it is invalid to close the memory stream, and you can still call the method of this class after closing. The close () of the underlying source code is an empty method

①, byte memory stream: ByteArrayOutputStream, ByteArrayInputStream

/ / Byte array output stream: program-"memory ByteArrayOutputStream bos = new ByteArrayOutputStream (); / / write data to memory bos.write (" ABCD ".getBytes ()); / / create a newly allocated byte array. Its size is the current size of this output stream, to which the valid contents of the buffer have been copied. Byte [] temp = bos.toByteArray (); System.out.println (new String (temp,0,temp.length)); byte [] buffer = new byte [10]; / / Byte array input stream: memory-"Program ByteArrayInputStream bis = new ByteArrayInputStream (temp); int len =-1" While ((len=bis.read (buffer))! =-1) {System.out.println (new String (buffer,0,len));} / / it's okay if you don't write it here, because close () in the source code is an empty method body bos.close (); bis.close ()

②, character memory stream: CharArrayReader, CharArrayWriter

/ / character array output stream CharArrayWriter caw = new CharArrayWriter (); caw.write ("ABCD"); / / returns a copy of memory data char [] temp = caw.toCharArray (); System.out.println (new String (temp)); / / character array input stream CharArrayReader car = new CharArrayReader (temp); char [] buffer = new char [10]; int len =-1 While ((len=car.read (buffer))! =-1) {System.out.println (new String (buffer,0,len));}

③, string stream: StringReader,StringWriter (temporarily stores data in a string)

/ / string output stream, the underlying layer uses StringBuffer to concatenate StringWriter sw = new StringWriter (); sw.write ("ABCD"); sw.write ("handsome pot"); System.out.println (sw.toString ()); / / ABCD handsome pot / / string input stream StringReader sr = new StringReader (sw.toString ()); char [] buffer = new char [10] Int len=-1; while ((len=sr.read (buffer))! =-1) {System.out.println (new String (buffer,0,len)); / / ABCD Shuai Pot} 5, merge stream: merge multiple input streams into one stream, also called sequential stream, because the first stream is read when reading, and the next stream is read after reading.

/ / define byte input merge stream SequenceInputStream seinput = new SequenceInputStream (new FileInputStream ("io/a.txt"), new FileInputStream ("io/b.txt")); byte [] buffer = new byte [10]; int len=-1; while ((len=seinput.read (buffer))! =-1) {System.out.println (new String (buffer,0,len)) } seinput.close (); Thank you for reading this article carefully. I hope the article "sample Analysis of Packaging flow in Java IO" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report