Io in Java used to look so confusing, now comb
The 1.IO stream is divided into two categories, one is the end of stream, which is called the byte stream, as the name implies, the transmission in bytes, and the other is a character stream ending in reader and writer, which seems to encapsulate the end of the stream.
IO Stream class, and another function similar, but the transmission media is no longer a byte, but the character, that is, according to the legend of different characters, such as UTF-8,GBK, its transmission unit is not fixed.
2. Input and output, input refers to read from the file into memory, output refers to the memory of the content written out to the file
3. Common buffered-beginning classes, such as (bufferedinputstream/bufferedoutputstream) and (bufferedinputreader/ bufferedoutputwriter) is the application of buffered optimization speed
and the resulting class, for example, is so used
<span style= "White-space:pre" ></span>outputstream os = new FileOutputStream (New File ("D:/1.txt"));// Create the underlying output stream bufferedoutputstream bos = new Bufferedoutputstream (OS);//wrapper base stream OutputStream
This will greatly increase the speed of the output.
4. Attach a structure diagram
(The server is not, come back to pass)
5. In fact, these streams can be divided into the basic stream and the packaging flow, the underlying stream is directly responsible for reading the read-out, when declaring the object, as the most internal layer of parameters, the wrapper flow is equivalent to better use the underlying stream and the base
Stream as a parameter to declare the constructor's
1) in byte stream: Fileinputstream/outputstream is the underlying stream that contains
int read ()//reads a byte if the end of the file returns -1;int read (byte [] b)//read B.length bytes written to b[], int read (byte[] b,int begin,int len)// From begin read Len length write b[] void write () void write (byte[] b)
and Datainputstream/dataoutputstream and Bufferedinputstream/bufferedoutputstream are his packaging streams, which means they can fileinputstream/ OutputStream as a structure
Parameters to declare the object, Datainputstream/dataoutputstream has some functions to support multiple types of input and output. Bufferedinputstream/bufferedoutputstream is the use of buffering
Speed of Read and write, and a flush () function that is responsible for writing the buffer's files to the underlying input and output
2) Filereader/filewriter provides the character reading of the file in the character stream, and the usage and fileinputstream/outputstream are basically consistent
InputStreamReader, OutputStreamWriter but fileinputstream/outputstream packaging Flow!! Be sure to pay attention, why do you have this? It's for the transmission on steam.
Time can be different encoding, to solve the garbled problem. Bufferedreader/bufferedwriter is the packing flow of the above groups, compared . Bufferedinputstream/bufferedoutputstream, one more
A ReadLine (), newLine () this whole line function
6. Give me a chestnut.
OutputStream OS = new FileOutputStream (New File ("D:\\a.txt"));D ataoutputstream dos = new DataOutputStream (OS);// Packaging Foundation Flow OutputStream
File input and output
import java.io.*;p ublic class Testfisfos {public static void main (string[] args) { InputStream is = null; OutputStream OS = null; The file for the try {//input stream must exist as is = new FileInputStream ("D:\\a.txt"); Char j = (char) is.read ();//Read a byte from A.txt, return the corresponding code value, strong turn System.out.println (j); The file for the output stream does not have to exist//true to indicate an append on the source file, and false to overwrite. The default is False OS = new FileOutputStream (New File ("D:\\a.txt"), true); Os.write (43);//write to B.txt a "+" number} catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } finally {try {is.close ();//Close input stream os.close ();//close output stream} catch (IOE Xception e) {e.printstacktrace (); } } }}
</pre> application buffer, wrapper stream <pre name= "code" class= "Java" >
Import java.io.*;p Ublic class Testbisbos {public static void Main (string[] args) throws IOException { Inputstrea M is = new FileInputStream (New File ("D:/src.rar"));//create the underlying input stream Bufferedinputstream bis = new Bufferedinputstream (IS) ;//Packaging Foundation Flow InputStream
OutputStream OS = new FileOutputStream (New File ("D:/dest.rar"));//create underlying output stream bufferedoutputstream bos = new Bufferedoutputstream (OS);//package Base stream OutputStream <span style= "White-space:pre" ></span>
<span style= "White-space:pre" ></span>int i = 0; while ((i = Bis.read ())!=-1) {//judgment is not read to the end of the file// after the buffer is full, do not have to flush the brush again immediately. will also automatically write the file to go to bos.write (i); } Bos.flush ();//The file is flushed from the buffer to the file to bos.close ();//Before closing, also flush the following /** * 1 not flush, not close then copy the file byte number relatively small, because, When the buffer is full, it will be automatically written to the * file. If the last 5 K, not enough he flush once, then left it in the buffer, so less * 2:flush write to the loop, no meaning, and the previous fileinputstream as slow. Because that's equivalent to reading a * byte into the buffer. It's been brushed. */ }}
Character stream input and output, packaging stream with packaging flow, to determine the format of characters, avoid garbled
<pre name= "code" class= "Java" >public class Testbwbr {public static void Main (string[] args) throws IOException { File File = new file ("D:\\a.txt");//create base file fileinputstream fis = new FileInputStream (File);//create the underlying input stream InputStreamReader ISR = new InputStreamReader (FIS);//Create a layer of wrapper flow bufferedreader br = new BufferedReader (ISR);//Create a two-tier wrapper flow String str = br.readline ();//Read Line System.out.println (str);
<span style= "White-space:pre" ></span>fileoutputstream fos = new FileOutputStream (file);//create the underlying output stream OutputStreamWriter OSW = new OutputStreamWriter (FOS);//Create a layer of wrapper flow bufferedwriter bw = new BufferedWriter (OSW);// Create a two-layer wrapper stream bw.write ("AAA");//write the String bw.newline ();//Enter a newline bw.write ("bbbb");//write a line Bw.flush (); Bw.close (); Br.close (); }}
The relationship and description of various IO in Java