In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use IO system in Java language". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Input and Output
1. A stream represents any data source capable of producing data, or any receiving source capable of receiving data. In Java IO systems, all streams (including Input and Out streams) consist of two types:
1.1 Byte-oriented stream
Byte-oriented stream means reading information from or writing information to a stream in bytes. Byte-oriented streams include the following types:
input
stream:
1)ByteArrayInputStream: Use a buffer in memory as an InputStream
2)StringBufferInputStream: Take a String object as an InputStream
3)FileInputStream: Take a file as an InputStream and read the file
4)PipedInputStream: implements the concept of pipe, mainly used in threads
5)SequenceInputStream: Combines multiple InputStreams into one InputStream
Out
stream
1)ByteArrayOutputStream: stores information in a buffer in memory
2)FileOutputStream: store information in a file
3)PipedOutputStream: implements the concept of pipe, mainly used in threads
4)SequenceOutputStream: Merge multiple Outstreams into one OutStream
1.2 Unicode character-oriented stream
Unicode character-oriented stream means reading from or writing to a stream in Unicode character units. Unicode character-oriented streams include the following types:
Input
Stream
1)CharArrayReader: corresponds to ByteArrayInputStream
2)StringReader: corresponds to StringBufferInputStream
3)FileReader: corresponds to FileInputStream
4)PipedReader: corresponds to PipedInputStream
Out
Stream
1)CharArrayWrite: corresponds to ByteArrayOutputStream
2)StringWrite: No corresponding byte-oriented stream
3)FileWrite: corresponds to FileOutputStream
4)PipedWrite: corresponds to PipedOutputStream
A character-oriented stream basically has a byte-oriented stream corresponding to it. The two corresponding classes implement the same function, and the word is oriented differently when operating. For example, CharArrayReader: and ByteArrayInputStream both use a buffer in memory as an InputStream, the difference being 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-existent oriented streams
InputStreamReader and OutputStreamReader: Convert a byte-oriented stream into a character-oriented stream.
2. stream Add Properties
2.1 The role of "add attributes to stream"
Using the Java API for operating IO described above, we can do whatever we want. But with subclasses of FilterInputStream and FilterOutStream, we can add attributes to streams. Here is an example of how this function works.
If we want to write data to a file, we can do this:
FileOutStream fs = new FileOutStream("test.txt");
Then you can write data to and from the test.txt file by calling the write() function from the generated fs object. However, if we want to implement the function of "caching the data to be written to the file first into memory, and then writing the cached data to the file", none of the above APIs can meet our needs. But by subclassing FilterInputStream and FilterOutStream, we 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 primitive type (int, char, etc.) data from stream.
2)BufferedInputStream: Use buffer
3)LineNumberInputStream: records the number of rows in the input stream, and then you can call getLineNumber() and setLineNumber(int)
4)PushbackInputStream: rarely used, generally used for compiler development
2.2.2 Used to encapsulate character-oriented InputStream
1)No class corresponding to DataInputStream. Use DataInputStream unless you want to use BufferedReader instead of readLine()
2)Buffered Reader: corresponds to Buffered InputStream
3)LineNumberReader: corresponds to LineNumberInputStream
4)PushBackReader: corresponds to PushBackInputStream
2.3 Various types of FilterOutStream
2.2.3 Used to encapsulate byte-oriented OutputStream
1)DataIOutStream: Outputs primitive type (int, char, etc.) data into stream.
2)Buffered OutStream: Use Buffer
3)PrintStream: Generate formatted output
2.2.4 Used to encapsulate character-oriented OutputStream
1)BufferedWrite: correspond to
2)PrintWrite: corresponds to
3. RandomAccessFile
1)Read and write operations on files can be completed through RandomAccessFile objects
2)When generating an object, you can specify the nature of the file to be opened: r, read-only;w, write-only;rw read-write
3)You can jump directly to the specified location in the file
4. An example of an I/O application
Java code
import java.io.*; public class TestIO{ public static void main(String[] args) throws IOException{ //1. Read data from a file in rows 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 keyboard input BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a line:"); System.out.println(stdin.readLine()); //2. Reading 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. format 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. output to file 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. File manipulation via RandomAccessFile RandomAccessFile rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw"); for(int i=0; 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.