In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to understand the IO system in the Java language. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Input and Output in Java IO system
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 system, all stream (including Input and Out stream) include two types:
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:
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
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:
Input
Stream
1) CharArrayReader: corresponding to ByteArrayInputStream
2) StringReader: corresponding to StringBufferInputStream
3) FileReader: corresponding to FileInputStream
4) PipedReader: corresponding to PipedInputStream
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.
Conversion between two kinds of undirected stream in 1.3Java IO system
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. 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 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 in Java IO system
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
An example of 4.Java IO system Application
Java code
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:\\ nepalon\\ TestIO.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:writeChars\ n")
Out2.writeBytes ("Thas was pi:writeByte\ n")
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.
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.