Java Io knowledge byte stream base class inputstreamoutputstream bytes stream base class readerwriter is a writer subclass object used to operate files. filewriter is suffixed with the parent class name and the prefix name is the function of the stream object import Java. io. * Public static void main (string ARGs []) {1. Create a filewriter object. When the object is initialized, the file filewriter fw = new filewriter ("demo.txt") to be operated is required "); // This file will be created under the specified directory. If there is already a file with the same name under this directory, it will be overwritten 2, write data FW. write ("123456"); // write the string to the stream. In the memory zone 3, write the data in the stream to the file FW. flush (); // refresh only, not close. You can use this stream later. // FW. close (); // This method can also write data to a file, but the stream will be closed, and no more Processing Method of Io exception} public static void main (string ARGs []) {filewriter fw = NULL; try {fw = new filewriter ("demo.txt"); FW. write ("123456");} catch (ioexception e) {try {If (FW! = NULL) FW. close ();} catch (ioexception e) {system. out. println (E. tostring () ;}} writes filewriter fw = new filewriter ("demo.txt", true) to an existing file appended; // initializes the constructor and can choose whether to overwrite the file FW. writes ("123456"); FW. close (); read the file // 1, create a file to read the object, and associate it with the specified file item. Ensure that the file already exists, if no IO exception is thrown, filereader Fr = new filereader ("demo.txt"); // 2 read the file Fr. read (); read only one character at a time, you can continue to read; Fr. close (); // Read File filereader Fr = new filereader ("demo.txt"); int CH = 0; while (CH = Fr. R EAD ()! =-1) {system. out. println ("ch:" + (char) CH);} // read filereader Fr = new filereader ("demo.txt") in string mode "); char [] Buf = new char [1024]; int num = 0 while (num = Fr. read (BUF )! =-1) {system. out. println (new string (BUF, 0, num);} Fr. close (); file copy 1, one-character copy filewriter fw = new filewriter ("democopy.txt"); filereader Fr = new filereader ("demo.txt") int CH = 0; while (CH = Fr. read ()! =-1) {FW. write (CH);} Fr. close (); FW. close (); 2, copy filewriter fw = new filewriter ("democopy.txt"); filereader Fr = new filewriter ("demo.txt "); char [] Buf = new char [1024]; int num = 0; while (num = Fr. read (BUF ))! =-1) {FW. write (BUF, 0, num);} Fr. close (); FW. close (); the buffer zone appears to improve the operation efficiency of the stream. Therefore, during initialization, the stream object must copy a file bufferwriter bufw = new bufferwriter (New filewriter ("democopy.txt") through the buffer zone ")); bufferreader bufr = new bufferreader (New filereader ("demo.txt"); string line = NULL; while (line = bufr. readline ())! = NULL) // The Readline method reads a row of data and returns the string {bufw. write (line); bufw. newline (); bufw. flush ();} bufw. close (); bufr. close (); Decoration Design mode when you want to enhance the functionality of an existing object, you can define a class to pass in the existing object, based on the functions of the existing object, and provide enhanced functionality, then, the custom decoration class is called the decoration class. It usually receives the decorated objects through the constructor. Based on the functions of the decorated objects, it provides richer functions. The decoration mode is more flexible than the inheritance mode, the code is not bloated, and the relationship between classes is reduced. The decoration class enhances existing objects and has the same functions as existing ones, but provides stronger functions, therefore, the decoration class and the decorated class usually belong to the inheritance relationship of reader in a system. | -- textreader | -- buffertextreader | -- mediareader | -- buffermediareader |- -Filereader | -- bufferfilereader | -- webreader | -- bufferwebreader reader decoration relationship | -- textreader | -- mediareader | -- filereader | -- webreader | -- bufferreader decoration is better than inheritance: reduces code bloated and enhances code readability class source primitive class {public void function () {}} class supersource decorative class {source S = NULL; Public supersource (source S) {This. S = s;} public void superfunction () {code s to be operated. function (); Code to be operated} byte stream inputstreamoutputstream // Write File fileoutp Utstream Fos = new fileoutputstream ("demo.txt"); FOS. write ("abcdef ". getbytes (); // The byte stream does not require flush () to refresh FOS. close (); // read the file fileinputstream FCM = new fileinputstream ("demo.txt"); int CH = 0; while (CH = FCM. read ())! =-1) {system. out. prinln (char) CH);} fiis. close (); fileinputstream FCM = new fileinputstream ("demo.txt"); byte [] Buf = new byte [1024]; int Len = 0; while (LEN = FCM. read (BUF )! =-1) {system. out. println (new string (BUF, 0, Len);} FCM. close (); FCM. available (); get the file size byte [] Buf = new byte [FCM. avaliable ()] // The VM initialization size is 64mfis. read (BUF); FCM. close (); byte stream fileinputstreamfileoutputstream bufferedinputstreambufferedoutputstream class streamdemo {// input stream bufferedreader BR = new bufferedreader (New inputstreamreader (system. in); // system if the input stream is a file. replace in with new fileinputstream ("filepath") // output stream buff Eredwriter BW = new bufferedwriter (New outputstreamwriter (system. out); // system if the output stream is a file. replace out with new fileoutputstream ("filepath") // The input stream reads while (string line = BR. readline ()! = NULL) {// output stream Print Output BW. Write (line); BW. newline (); BW. Flush ();}
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