Day21 read-write data in the Java language (ii)

Source: Internet
Author: User

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)

Related Article

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.