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

How to understand the IO system of Java

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to understand the IO system of Java. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1.1 Byte-oriented stream

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

1) inputstream:

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) Outputstream

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

Type:

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. Such as

CharArrayReader: the function of both CharArrayReader and ByteArrayInputStream is to use a buffer in memory as InputStream, except that the former reads one from memory at a time

Bytes of information, which reads one character at a time from memory.

[@ more@] 1.3 conversion between two undirected 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 attributes to the stream. Here is an example 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 "first cache the data to be written to the file to memory first"

When writing the cached data to a file, none of the above API can meet our needs. But through FilterInputStream and FilterOutStream's

Subclass to add the functionality we need for 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:nepalonTestIO.java"))

String s, S2 = new String ()

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

S2 + = s + "n"

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:nepalon 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:nepalon Data.txt")

Out2.writeDouble (3.1415926)

Out2.writeChars ("nThas was pi:writeCharsn")

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

Out2.close ()

DataInputStream in5 =

New DataInputStream (

New BufferedInputStream (

New FileInputStream ("F:nepalon 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:nepalon 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