IO streams (character stream FileReader)
Jifuryu:
* Character stream that can read and write IO streams characters directly.
* Character stream to read characters, you must first read byte data, and then converted to a character, if you want to write a character, you need to re-byte character to write.
Under what circumstances the use of character streams:
* Character stream can also copy files, but not recommended, because the characters will Byte to read, write Shihai put back byte characters.
* Program needs to read a text, or need to write a piece of text can be used when a character stream
* When reading is read in accordance with the size of the characters, does not appear half Chinese.
* Write when you can write directly to the string, not into a byte array.
Precautions:
* Can not copy files that are not in plain text. (File garbled)
IO streams (character stream FileWriter)
* FileWriter class write () method can be automatically converted to character bytes written.
public static void main (String [] args) throws IOException { // A method of automatically generating stubs TODO FileWriter FW = new new FileWriter ( "eee.txt"); // Create File fw.write ( "a happy June"); / / the Write (): the characters into bytes. fw.close (); // release resources }
IO streams (character stream copy)
public static void main (String [] args) throws IOException { the FileReader fr = new new the FileReader ( "test.txt" ); FileWriter FW = new new FileWriter ( "eeecc.txt" ); int B; the while ((B = fr.read ! ()) = -1 ) { fw.write (B); } fr.close (); fw.close (); // must be closed, if none of the stream, it will write the contents of the buffer, flow off It will refresh the buffer contents, then close. }
IO streams (custom character array copies)
public static void main(String[] args) throws IOException { FileReader list = new FileReader("test.txt"); FileWriter list2 = new FileWriter("copy.txt"); char[] arr = new char[1024]; int len; while ((len = list.read(arr)) != -1) { list2.write(arr, 0, len); } list.close(); list2.close(); }