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

What are the knowledge points related to JAVA's IO stream?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points related to JAVA's IO stream, which are detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.

I. input and Output

1.stream represents any data source that has the ability to produce data, or any receiving source that has the ability to receive data.

In Java's IO, all stream (including Input and Out stream) include two types:

Byte-oriented stream A byte-oriented stream that indicates that information is read from or written to stream in bytes. Byte-oriented stream includes the following types:

1) input stream:

1) ByteArrayInputStream: use a buffer in memory as InputStream

2) StringBufferInputStream: use a String object as an InputStream

3) FileInputStream: use a file as InputStream to read the file

4) PipedInputStream: implements the concept of pipe, which is mainly used in threads

5) SequenceInputStream: merge multiple InputStream into a single InputStream

2) Out stream

1) ByteArrayOutputStream: store the information in a buffer in memory

2) FileOutputStream: store the information in the file

3) PipedOutputStream: implements the concept of pipe, which is mainly used in threads

4) SequenceOutputStream: merge multiple OutStream into a single OutStream

1.2 Unicode character-oriented stream

Stream, which is guided by Unicode characters, means that information is read from or written to stream in units of Unicode characters. Unicode character-oriented stream includes the following types:

1) Input Stream

1) CharArrayReader: corresponding to ByteArrayInputStream

2) StringReader: corresponding to StringBufferInputStream

3) FileReader: corresponding to FileInputStream

4) PipedReader: corresponding to PipedInputStream

2) Out Stream

1) CharArrayWrite: corresponding to ByteArrayOutputStream

2) StringWrite: there is no corresponding byte-oriented stream

3) FileWrite: corresponding to FileOutputStream

4) PipedWrite: corresponding to PipedOutputStream

Character-oriented stream basically has its corresponding byte-oriented stream. The two corresponding classes implement the same function, and the word is guided differently in the operation. For example, CharArrayReader: and ByteArrayInputStream both use a buffer in memory as InputStream, except that the former reads one byte of information from memory at a time, while the latter reads one character from memory at a time.

1.3 conversion between two non-presence-oriented stream

InputStreamReader and OutputStreamReader: convert a byte-oriented stream into a character-oriented stream.

2. Stream add attributes

2.1 the role of "adding attributes to stream"

Using the API that operates IO in the Java described above, we can do whatever we want. But through subclasses of FilterInputStream and FilterOutStream, we can add properties to stream. The following is a

An example is given to illustrate the role of this function.

If we want to write data to a file, we can do this:

FileOutStream fs = new FileOutStream ("test.txt")

The write () function can then be called through the resulting fs object to write data back and forth to the test.txt file. However, if we want to implement the function of "first cache the data to be written to the file first, and then write the cached data to the file", none of the above API can meet our needs. But through subclasses of FilterInputStream and FilterOutStream, add the functionality we need to FileOutStream.

2.2 various types of FilterInputStream

2.2.1 used to encapsulate byte-oriented InputStream

1) DataInputStream: reads data of basic types (int, char, etc.) from stream.

2) BufferedInputStream: using buffers

3) LineNumberInputStream: the number of rows in the input stream is recorded, and then getLineNumber () and setLineNumber (int) can be called

4) PushbackInputStream: rarely used, generally used for compiler development

2.2.2 used to encapsulate character-oriented InputStream

1) there is no corresponding class for DataInputStream. Use DataInputStream unless you want to use BufferedReader instead of readLine ()

2) BufferedReader: corresponding to BufferedInputStream

3) LineNumberReader: corresponding to LineNumberInputStream

4) PushBackReader: corresponding to PushbackInputStream

2.3 various types of FilterOutStream

2.2.3 used to encapsulate byte-oriented OutputStream

1) DataIOutStream: output basic type (int, char, etc.) data to stream.

2) BufferedOutStream: using buffers

3) PrintStream: generate formatted output

2.2.4 used to encapsulate character-oriented OutputStream

1) BufferedWrite: corresponding to

2) PrintWrite: corresponding to

3. RandomAccessFile

1) you can read and write the file through the RandomAccessFile object

2) when generating an object, you can specify the nature of the file to be opened: r, read-only; w, write-only; rw can read and write

3) you can jump directly to the location specified in the file

4. An example of the application of Icano

Import java.io.*

Public class TestIO {

Public static void main (String [] args)

Throws IOException {

/ / 1. Read data from a file in behavioral units

BufferedReader in =

New BufferedReader (

New FileReader ("F: epalonTestIO.java"))

String s, S2 = new String ()

While ((s = in.readLine ())! = null)

S2 + = s + ""

In.close ()

/ / 1b. Receive input from the keyboard

BufferedReader stdin =

New BufferedReader (

New InputStreamReader (System.in))

System.out.println ("Enter a line:")

System.out.println (stdin.readLine ())

/ / 2. Read data from a String object

StringReader in2 = new StringReader (S2)

Int c

While ((c = in2.read ())! =-1)

System.out.println ((char) c)

In2.close ()

/ / 3. Remove formatted input from memory

Try {

DataInputStream in3 =

New DataInputStream (

New ByteArrayInputStream (s2.getBytes ())

While (true)

System.out.println ((char) in3.readByte ())

}

Catch (EOFException e) {

System.out.println ("End of stream")

}

/ / 4. Export to Fil

Try {

BufferedReader in4 = new BufferedReader (new StringReader (S2))

PrintWriter out1 =

New PrintWriter (

New BufferedWriter (

New FileWriter ("F: epalon TestIO.out")

Int lineCount = 1

While ((s = in4.readLine ())! = null)

Out1.println (lineCount++ + ":" + s)

Out1.close ()

In4.close ()

}

Catch (EOFException ex) {

System.out.println ("End of stream")

}

/ / 5. Storage and recovery of data

Try {

DataOutputStream out2 =

New DataOutputStream (

New BufferedOutputStream (

New FileOutputStream ("F: epalon Data.txt")

Out2.writeDouble (3.1415926)

Out2.writeChars ("Thas was pi:writeChars")

Out2.writeBytes ("Thas was pi:writeByte")

Out2.close ()

DataInputStream in5 =

New DataInputStream (

New BufferedInputStream (

New FileInputStream ("F: epalon Data.txt")

BufferedReader in5br =

New BufferedReader (

New InputStreamReader (in5))

System.out.println (in5.readDouble ())

System.out.println (in5br.readLine ())

System.out.println (in5br.readLine ())

}

Catch (EOFException e) {

System.out.println ("End of stream")

}

/ / 6. Manipulate files through RandomAccessFile

RandomAccessFile rf =

New RandomAccessFile ("F: epalon rtest.dat", "rw")

For (int item0; I

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