Concept of Flow:
1. An in Putstream that operates on a byte. out Putstream class
in is the reading data, out is the output data, the number is easy to confuse.
The most common subclasses of InputStream and OutputStream
FileInputStream and FileOutputStream working with byte data
DataInputStream can be used in binary operation
2. Reader.writer class for string manipulation
Reader.writer subclasses Bufferedreader.bufferedwrite can manipulate strings
-----------------------then explain the two concepts in detail----------------------------
- Architecture of the InputStream
- InputStream method
- Architecture of the OutputStream
- OutputStream method
InputStream Architecture
InputStream method
InputStream method
int read () |
Reads a single byte from the input stream, returning the read byte data |
int read (byte[] b) |
Reads bytes up to B.leng length from the input stream, stored in byte array B, returning the number of bytes read in time. |
int read (byte[] b, int off, int len) |
Reads a long len length byte from the input stream, saved in byte array B, and the saved position starts from off |
void Close () |
Close the input stream |
int available () |
Returns the bytes that can be read from the input stream in writing |
Skip (long N) |
Skips parameter n from the input stream to set the number of bytes |
Mark (int readlimit) |
Mark the current position of the input stream and reset it to the location of the marker with the Reset () method again |
void Reset () |
The current position is reset to the location where the mark () method was last called |
|
|
The construction method of FileInputStream
FileInputStream (File file) |
file specifies the source of the files data |
FileInputStream (String name) |
Name specifies the file data source, including the path information |
Sample code
1 /*Package whatever,//don ' t place package name!*/2 3 ImportJava.util.*;4 Importjava.lang.*;5 ImportJava.io.*;6 7 /*the Name of the class has been "Main" only if the class was public.*/8 classInputStream9 {Ten Public Static voidMain (string[] args)throwsjava.lang.Exception One { AFileInputStream FIS =NewFileInputStream ("H:\\app\\test.txt"); - - intdata; the -System.out.println ("Number of bytes readable:" +fis.available ()); - - //Convert bytes to Strings + while(data = Fis.read ())!=-1){ - + Charc = (Char) data; A at System.out.println (c); - } - - fis.close (); - } -}
OutputStream Architecture
OutputStream method
void write (int c) outputs the specified bytes to the input stream
void Write (byte[] buf) outputs the byte array to the output stream
void Write (byte[] b, int off, int len) starts the off position in the byte array, and the byte data with Len length is output to the output stream
void Close () to close the output stream
void Flush () force bar Any buffered write data output to the output stream
FileOutputStream method
The FileOutputStream (file file) file file name specifies that the files receive data Objects
FileOutputStream (String name) name Specifies the receive object that contains the path information
FileOutputStream (String name, Boolean append) Append if true, adds data at the end of the file.
Sample code
1 ImportJava.util.*;2 Importjava.lang.*;3 ImportJava.io.*;4 5 /*the Name of the class has been "Main" only if the class was public.*/6 classOutputStream7 {8 Public Static voidMain (string[] args)9 {Ten One Try{ A -String str = "This is an example of an output stream" - the byte[] words = str.getbytes ();//decodes the STR string into a byte array. - - FileOutputStream Fos; - +FOS =NewFileoutputstram ("H:\\app\\test.txt"); - +Fos.write (words,0, words.length); A atSystem.out.println ("Prompt file has been updated"); - //Remember to close the output stream - fos.close (); - -}Catch(FileNotFoundException e) { - e.printstacktrace (); in}Catch(IOException e) { -SYSOUT.OUT.PRINTLN ("Error creating file"); to } + - } the}
Reader architecture
Reader method
int read () reads a single character from the input stream, returning the character data that was read
int read (char[] c) reads C.length-length characters from the input stream, saves to character array C, returns the number of characters actually read
int read (char[] c, int off, int len) reads the length character of up to Len from the input stream, saves it in the character array C, and saves the position from
Off position, returning the actual read string length
void Close () Close stream
Boolean ready () returns true if the stream to be read is already prepared, otherwise false
Skip (long N) skips parameter n specified number of characters from the input stream
Mark (int readlimit) marks the current position in the input stream so that reset () resets to the tag
void Reset () resets the current position to the location where the mark () method was last called
The construction method of BufferedReader
BufferedReader (reader in) parameter in specifies the decorated Reader class
BufferedReader (Reader in, int sz) parameter sz specifies the size of the buffer, in units of characters.
Sample code
1 classCharreder2 {3 Public Static voidMain (string[] args)4 {5 Try{6 7FileReader FR =NewFileReader ("H:\\app\\test.txt");8 9Buferedreader br =NewBufferedReader (FR);Ten OneString line =br.readline (); A while(line!=NULL){ - - System.out.println (line); theline =br.readline (); - - } - br.close (); + fr.close (); -}Catch(IOException e) { +SYSOUT.OUT.PRINTLN ("File does not exist"); A } at } -}
The construction method of BufferedWriter
BufferedWriter (writer out) parameter out specifies the decorated writer class
BufferedWriter (Writer out, int sz) parameter sz specifies the size of the buffer, in units of characters
Sample code
1 classCharwriter2 {3 Public Static voidMain (string[] args)4 {5 Try{6 7 FileWriter FW;8 9FW =NewFileWriter ("H:\\app\\test.txt");Ten OneBufferedWriter BW =NewBufferedWriter (FW); ABw.write ("Hello, Sao Year"); -Bw.write ("I am ....")); - Bw.flush (); the fw.close (); -}Catch(IOException e) { -SYSOUT.OUT.PRINTLN ("File does not exist"); - } + } -}
Input and output streams in Java