Character Stream and Byte stream

Source: Internet
Author: User


/*
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.