IO streams are used to process data transfers between devices. File copying, uploading and downloading files are possible.
IO Stream classification
IO streams can store data in a file or read data from a file
Output stream (for write data)
FileWriter
voidWrite (String str)//write a String datavoidWrite (String str,intIndexintLen//write a portion of the data in a stringvoidWriteintCh//write a character data, write int type the benefit is that you can write either char type data or write to insert the corresponding int type datavoidWriteChar[] CHS)//write a character datavoidWriteChar[] CHS,intIndexintLen//write a character array part
Steps
Write data to a file (out of the box for the CPU)--The steps to write data with the output stream-->--> FileWriter output stream: A. Creating an output stream object B. Method of invoking the output stream object to write data C. Freeing resources
Construction method, creating an output stream object
FileWriter (String fileName)//filename The file path does not write the drive letter as a relative path (relative to the item)
FileWriter (String Filename,boolean append)//output to file when added to end of file
Member Methods
void write (String str)//Call Stream object Write data method
The data is not written directly to the file, it is actually written to the memory buffer
void flush ()//writes data from a memory buffer to a file
void Close ()//closes the stream, otherwise it will always occupy files that cannot be manipulated.
Input stream (for reading data)
FileReader
Construction Method The FileReader (String fileName) member method int read () //reads one character at a time, and returns the int value of the character. If there is no data, return-1 int Read (char[] cbuf)
Steps
A. Creating an input stream B. Invoking the Read data method of an input stream object c. Closing a stream
difference between close () and flush () methods
A.flush () flushes the buffer. The Stream object can also continue to be used after the refresh
B.close () flushes the buffer first and then notifies the system to release the resource. Stream objects can no longer be used.
How to wrap characters in a text file when the Mac is \r,linux Yes \n,windows is \ r \ n
The file is copied ( transmitted ) by first reading the data with FileReader, and then using FileWriter to write the data. first I in O.
//creating an input stream objectFileReader FR =NewFileReader ("Source.txt"); //creating an output stream objectFileWriter FW =NewFileWriter ("Target.txt"); /*read/write data int ch; while ((Ch=fr.read ())!=-1) {//read to the return character int value, cannot read return-1 fw.write (CH); }*/ //Read and write Data Char[] CHS =New Char[1024];//Maximum character array intLen; while(Len=fr.read (CHS))!=-1) {//returns the array length when reading to a character array, not reading back-1Fw.write (CHS, 0, Len); } //Freeing ResourcesFw.close (); Fr.close ();
Java Foundation-io Stream (123