One . Input and Output
 

        1.
stream Represents any data source capable of producing data , Or any receiving source capable of receiving data . stay Java Of IO in , be-all stream( include Input and Out
stream) There are two types :

        1.1 Byte oriented stream

   Byte oriented stream, Indicates that in bytes from stream Read from or to stream Write information in . Byte oriented stream Including the following types :

  1) input stream:

  1) ByteArrayInputStream: Take a buffer in memory as InputStream use

  2) StringBufferInputStream: Put one String Object as InputStream

  3) FileInputStream: Take a document as InputStream, Read file

  4) PipedInputStream: Achieved pipe Concept of , Mainly used in threads

  5) SequenceInputStream: Put more than one InputStream Merge into one InputStream

  2) Out stream

  1) ByteArrayOutputStream: Store information in a buffer in memory

  2) FileOutputStream: Store information in a file

  3) PipedOutputStream: Achieved pipe Concept of , Mainly used in threads

  4) SequenceOutputStream: Put more than one OutStream Merge into one OutStream

  1.2 with Unicode Character oriented stream

   with Unicode Character oriented stream, Indicated by Unicode Character as unit from stream Read from or to stream Write information in . with Unicode Character oriented stream Including the following types :

  1) Input Stream

  1) CharArrayReader: And ByteArrayInputStream corresponding

  2) StringReader: And StringBufferInputStream corresponding

  3) FileReader: And FileInputStream corresponding

  4) PipedReader: And PipedInputStream corresponding

  2) Out Stream

  1) CharArrayWrite: And ByteArrayOutputStream corresponding

  2) StringWrite: No byte oriented stream

  3) FileWrite: And FileOutputStream corresponding

  4) PipedWrite: And PipedOutputStream corresponding

   Character oriented stream Basically, there are corresponding byte oriented stream. The functions of two corresponding classes are the same , The orientation of words is different in operation . as CharArrayReader: and ByteArrayInputStream The function of is to use a buffer in memory as InputStream use , The difference is that the former reads one byte of information from memory at a time , The latter reads one character at a time from memory .

  1.3 Two kinds of invisible oriented stream Conversion between

  InputStreamReader and OutputStreamReader: Put a byte oriented stream Convert to a character oriented stream.

  2. stream Add attribute

  2.1 “ by stream Add attribute ” The role of

   Use the above Java Middle operation IO Of API, We can do whatever we want . But through FilterInputStream and FilterOutStream Subclass of , We can stream Add attribute . Here is an example to illustrate the function of this function .

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

FileOutStream fs = new FileOutStream(“test.txt”);

   And then you can generate fs Object call write() Function interaction test.txt Data written to file . however , If we want to achieve “ First, cache the data to be written to the file into memory , Then write the data in the cache to the file ” When , above API No one can meet our needs . But through FilterInputStream and FilterOutStream Subclass of , by FileOutStream Add features we need .

  2.2 FilterInputStream Various types of

  2.2.1 Used to encapsulate byte oriented InputStream

  1) DataInputStream: from stream Read basic type in (int,char etc. ) data .

  2) BufferedInputStream: Use buffer

  3) LineNumberInputStream: Will record input
stream Lines in , You can then call getLineNumber() and setLineNumber(int)

  4) PushbackInputStream: Rarely used , Generally used in compiler development

  2.2.2 Used to encapsulate character oriented InputStream

  1)
Not with DataInputStream Corresponding class . Unless you are using readLine() Use when BufferedReader, Otherwise use DataInputStream

  2) BufferedReader: And BufferedInputStream corresponding

  3) LineNumberReader: And LineNumberInputStream corresponding

  4) PushBackReader: And PushbackInputStream corresponding

  2.3 FilterOutStream Various types of

  2.2.3 Used to encapsulate byte oriented OutputStream

  1) DataIOutStream: to stream Basic type of medium output (int,char etc. ) data .

  2) BufferedOutStream: Use buffer

  3) PrintStream: Generate formatted output

  2.2.4 Used to encapsulate character oriented OutputStream

  1) BufferedWrite: Corresponding to

  2) PrintWrite: And correspondence

  3. RandomAccessFile

  1) Available through RandomAccessFile Object to read and write files

  2) When generating an object , Indicates the nature of the file to be opened :r, read-only ;w, Write only ;rw read-write

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

* 4. I/O An example of application

*  

* import java.io.*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1. Read data from a file in line 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 keyboard
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());

//2. From a String Object to read data from
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();

//3. Take formatting input out of 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 a 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. Data storage and recovery
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. adopt RandomAccessFile Operation file
RandomAccessFile rf =
new RandomAccessFile("F://nepalon// rtest.dat", "rw");
for(int i=0; i<10; i++)
rf.writeDouble(i*1.414);
rf.close();

rf = new RandomAccessFile("F://nepalon// rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();

rf = new RandomAccessFile("F://nepalon// rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();

rf = new RandomAccessFile("F://nepalon// rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();
}
}

   Code interpretation ( By area ):

  1 In District , When reading a file , First read the contents of the file into the cache , When called in.readLine() Time , Then read the data in the form of characters from the cache ( hereinafter referred to as “ Cache byte read mode ”).

  1b In District , Because you want to read from the standard as a cached byte IO( keyboard ) Read data in , So we need to set the standard first IO(System.in) Convert to character oriented stream, Go ahead again BufferedReader encapsulation .

  2 In District , In the form of characters from a String Object , So to produce a StringReader Of type stream.

  4 In District , Yes String object s2 When reading data , First, the data in the object is stored in the cache , Read from buffer ; Yes TestIO.out File operation , First output the formatted information to the cache , Then output the information in the cache to the file .

  5 In District , Yes Data.txt When the file is exported , First, output the basic type of data to the cache , Then output the data in the cache to the file ; When reading a file , First, read the data in the file into the cache , Read from the cache as a basic type . be careful in5.readDouble() This line . Because write the first writeDouble(), So in order to display correctly . Also read as a basic type .

  6 District is through RandomAccessFile Class to operate on files .
 

Technology
©2019-2020 Toolsou All rights reserved,
( Essence )2020 year 6 month 26 day C# Class library File read and write operation help class python Dynamic programming for single source shortest path Python realization switch method Result Encapsulation of return result information MySql sentence Recursively find all subordinate departments of an input department golang One line of code converts the slice into a semicolon separated string Vue Get the text and option value of the drop-down box vue use THREE.js Create a cube that you can control STM32 Infrared tracking car PostgreSQL: Nine . Indexes