Day20 read-write data in the Java language (i)

Source: Internet
Author: User

Day20 read-write data in the Java language (i)

I. Overview of IO

IO data stream read and write function, in real life is also very common, such as file upload, download, automatic log updates and so on is closely related to IO read and write. Io is also divided into two large functions of reading data and writing data. Here's a look at some of the types of data read.


650) this.width=650; "src=" Https://s4.51cto.com/oss/201711/11/9123078872f3bc1293d5ccf972d58a59.png "title=" io.png "alt=" 9123078872f3bc1293d5ccf972d58a59.png "/>

Second, several ways to read data

Reading data is divided into byte stream reading data, and character stream read data two.

(a): Character input stream:

1, FileReader class. is a character stream that reads data

2, BufferedReader class. Read data with buffered stream

(b): Byte input stream:

1, FileInputStream class. is a byte stream reading data

2, Bufferedinputstream. is to read data with buffered streams


Third, specific examples

(i) Character stream reading data

//io data stream input read "FileReader" package www.com.c1;import  Java.io.filenotfoundexception;import java.io.filereader;import java.io.ioexception;public class  io02 {public static void main (String[] args)  {TRY {//1, Create a Filereader filereader = new filereader ("D:\\zs.txt") using a character stream instance, and//2, define the variable to receive the read Int temp  = 0;//3, defines characters to convert. CHAR C;//4, Cyclic printing while  ((Temp = filereader.read ())!=-1)  {c = (char)  temp; System.out.print (c);} 5. Close data stream filereader.close ();}  catch  (filenotfoundexception e)  {e.printstacktrace ();}  catch  (ioexception e)  {e.printstacktrace ();}} 
IO stream input reads "BufferedReader" package Www.com.c1;import java.io.bufferedreader;import java.io.FileNotFoundException; Import Java.io.filereader;import Java.io.ioexception;public class Io04 {public static void main (string[] args) {try {//1, Create instance, BufferedReader buf = new BufferedReader (New FileReader ("D:\\zs.txt")), and//2, define string to receive string TEMP;//3, Using ReadLine to read the data, note that he is reading a line of string while ((temp = Buf.readline ())!=null) {System.out.println (temp);} 4. Close data stream buf.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}

(ii) byte stream reading data

package www.com.c1;import java.io.file;import java.io.fileinputstream;import  Java.io.filenotfoundexception;import java.io.ioexception;//io data stream input read "FileInputStream"//1, FileInputStream: Byte stream read file: Generally read some pictures, videos and so on.   Character Stream read files: It is usually some text. Public class io01 {public static void main (String[] args)  {//use the first way " Byte stream reads the file. TRY {//1 Step: Create an input byte stream instance, note the location path behind the file Fileinputstream inputstream01 = new fileinputstream ("D:\\zs.txt");//2 step, define variables to accept each read to the data INT TMP;//4, define variables to convert the print read what. CHAR OK;//3, using a while loop to read the contents of a file: Core step while  ((Tmp=inputstream01.read ())!=-1)  {ok =  (char)  tmp;//using coercion type conversion System.out.print (OK);} 5. Close data stream inputstream01.close ();}  catch  (filenotfoundexception e)  {system.out.println ("file does not exist!) Please create the file under the path change! ");}  catch  (ioexception e)  {e.printstacktrace ();} Use the second method to read the file. "Reading Chinese with a byte stream" try {//1, creating an instance of reading the file Fileinputstream inputstream = new fiLeinputstream (New file ("D:\\zs.txt"));//2. Define an array to control the amount of data read "like Cache" byte[] b = new byte[ 1024]; //3, define variables to receive read data INT A;//4, use Read[byte] method to read the data while  ((A = inputstream.read (b))! =-1)  {//5, create a string to receive processing read to the data, string s = new string (b, 0, a);//Pay attention to each parameter, Parameter one: Represents the maximum amount of data per read//Parameter two: Represents the offset of the read data, that is, from which byte begins to read the remaining data//parameter three: Read bytes of data. 6, Output System.out.println (s);} 7. Close data stream inputstream.close ();}  catch  (filenotfoundexception e)  {system.out.println ("File does not exist");}  catch  (ioexception e)  {e.printstacktrace ();}}
IO stream input read "Bufferedinputstream" package Www.com.c1;import Java.io.bufferedinputstream;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;public class Io03 {public static void Main (string[] args) {try {///1, create Bufferedinputstream instance, only one input stream, so the parent class or subclass of the input stream is created when it is created. Bufferedinputstream bis = new Bufferedinputstream (New FileInputStream ("D:\\zs.txt"));//2, define variable to receive the Read int temp = 0;//3, Defines characters to convert. Char C;//4, loop print while ((temp = Bis.read ())!=-1) {c = (char) temp; System.out.print (c);} 5. Close data stream bis.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}


Iv. concluding remarks

It's all here. There are 4 common ways to read files using IO streams. Note The application in these 4 instances.

This article from the "Program Ape" blog, reproduced please contact the author!

Day20 read-write data in the Java language (i)

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.