/*
Character stream and byte stream:
Two base classes of byte stream:
InputStream OutputStream
Two base classes of character streams:
Reader Writer
Learn the character stream character first.
Now that the IO stream is used to manipulate the data.
Then the most common form of data is: files.
Then the operation of the main file to display the first.
Requirement: On the hard disk, create a file and write some text data.
Found a writer subclass object specifically for manipulating files, FileWriter.
The suffix name is the parent class name, and the prefix name is the function of the stream object.
*/
Public classFilewriterdemo { Public Static voidMain (string[] args)throwsIOException {//1, create a FileWriter object, the object must be initialized to be clear to manipulate the file//and the file will be created to the specified directory, if the directory already has a file with the same name, it will be overwritten by the new. //in fact, the step is to identify the destination where the data will be stored. FileWriter FW =NewFileWriter ("Demo.txt"); //call the Write method to write the string to the stream. Fw.write ("ABCDE"); //closes the stream resource, but refreshes the data in the internal buffer one time before closing,//The data is brushed to the destination,//and flush difference: After flush refreshes, the stream can continue to be used, and when close is refreshed, the stream is closed. Fw.close (); //Refreshes the data in the buffer in the stream object,//brushes the data to the destination. //Fw.flush ();Fw.write ("Haha");//Fw.flush (); }}
Filewriterdemo
Second way: Read with a character array
Public classFileReaderDemo2 { Public Static voidMain (string[] args)throwsioexception{FileReader FR=NewFileReader ("Demo.txt"); //defines a character array that is used to store read characters. //the Read (char[]) returns the number of characters. Char[] buf =New Char[1024]; intnum = 0; while((Num=fr.read (BUF))!=-1) {System.out.println (NewString (buf,0, num)); } //int num = Fr.read (BUF); //System.out.println ("num+" +num+ "..." +new String (BUF)); //int num2 = Fr.read (BUF);// //System.out.println ("num2+" +num2+ "..." +new String (BUF));// //int num3 = Fr.read (BUF);// //System.out.println ("num3+" +num3+ "..." +new String (BUF,0,NUM3));////int num4 = Fr.read (BUF);// // //System.out.println ("num4+" +num4+ "..." +new String (BUF,0,NUM4));Fr.close (); }}
FileReaderDemo2
Character Stream and Byte stream