Day21 read-write data in the Java language (ii)
First, overview:
Using IO to write files is a function of downloading. So it is necessary to say it alone.
Second, write function: (download)
The Write function is also delimited for character Stream writes and Byte Stream writes in two ways.
(i): Character stream write data
1, FileWriter class.
2, BufferedWriter class. With buffered stream
(ii): byte stream Write data
1, FileOutputStream class.
2, Bufferoutputstream class. With buffered streams
Third, specific examples
(i): Character stream write data
IO data stream Output "FileWriter" Package Www.com.c2;import Java.io.filewriter;import Java.io.ioexception;public class Io06 { public static void Main (string[] args) {//1, preparing data to be written to String str = "My name is ls"; try {//2, create instance, write FileWriter FWA = N with append EW FileWriter ("D:\\ls.txt", true),//3, write Data fwa.write (str),//4, close data Fwa.close ();} catch (IOException e) {e.printstacktrace ();}}}
IO data stream Output "BufferedWriter" package Www.com.c2;import java.io.bufferedwriter;import Java.io.filewriter;import Java.io.ioexception;public class Io08 {public static void main (string[] args) {try {//1, create instance bufferedwriter bw = new Buffer Edwriter (New FileWriter ("D:\\ls.txt", true)); String str = "Hello!" I'm John Doe. ";///2, write Bw.write (str);//3, empty buffer Bw.flush ();} catch (IOException e) {e.printstacktrace ();}}
(ii) byte stream write data:
IO data stream output "FileOutputStream" package www.com.c2;import java.io.filenotfoundexception;import java.io.fileoutputstream;import java.io.ioexception;//output Stream/* Step: 1, prepare the data you need to write 2, create FileOutputStream object 3, write *///Note: When your file does not have a corresponding file under the specified path, the corresponding file is created. If there is a file, the data content is replaced by default Public class io05 {public static void main (string[] args)  {//1, prepare the data you need to write string str = "Hello";//Convert the string to a character array byte[] bt = Str.getbytes (); try {//default Replace Write//2, object to create FileOutputStream fileoutputstream fos = new FileOutputStream ("D:\\zs.txt");//3, default replacement Write fos.write (BT),//4, turn off data stream Fos.close (),//Append to write/ 2.1 are appended after data based on later data back. Similar to log management Fileoutputstream fo = new fileoutputstream ("D://ls.txt", true);// 2.2 Append Write Fo.write (BT);//2.3 Close data stream fo.close ();} catch (filenotfoundexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();}}
IO data stream Output "Bufferoutputstream" package Www.com.c2;import Java.io.bufferedoutputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;public class Io07 {public static void Main (string[] args) {try {//1, create instance appended after field Bufferedoutputstream Bops = new Bufferedoutputstream (new FileOutputStream ("D:\\ls.txt", true));//2, write Bops.write ("Hello!" ". GetBytes ());//3, emptying buffer Bops.flush ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}
Iv. Concluding remarks:
Here we will talk about the reading and writing of file data is finished, will be commonly used in these few learn to OK
This article from the "Program Ape" blog, reproduced please contact the author!
Day21 read-write data in the Java language (ii)