OutputStreamWriter Writing Data
█
OutputStreamWriter Write Data method
?Public void Write (int c); //write a single character. ?Public void write (char[] cbuf); //writes an array of characters. ?Public void write (char[] cbuf,int off,int len); //writes a portion of a character array. ?Public void write (String str); //write String. ?Public void write (String str,int off,int len); //write a part of the string.
█
problems to be noticed in character stream operation
?The role of Flush () ?The difference between flush () and close ()
public static void Main (string[] args) throws IOException {//character stream = byte stream + encoding table FileOutputStream fos = new FileOutputStream ("1.txt"); //No impact. If the file does not exist, it is created//fileinputstream fis = new FileInputStream ("2.txt"); //If the file does not exist, subsequent operations cannot be done//outputstreamwriter OSW = new OutputStreamWriter (FOS);//To write the default encoding table is the system default (utf-8) OutputStreamWriter OSW = new OutputStreamWriter (FOS, "utf-8"); Osw.write ("Hello! ");//Do not write flush cannot flush buffered write file, close Close file also has this functionOsw.write (' Medium ');//write single characterOsw.write ("First meeting, please take more care", 2, 3);//write a part of a string; start with the second character, write three altogether |
char[] ch = {' present ', ' Day ', ' Down ', ' rain '}; Osw.write (CH); //write array osw.write (CH, 2, 2); //Starting from the second character of the array, write 22 to buffer
Osw.flush (); //Refresh buffer to write data in the pipeline to file osw.close (); //Release resources, but flush} before release } |
Java--io class, character stream write data