Io stream text file read mode

Source: Internet
Author: User

/*

* First reading Mode

*/

Import java.io.*;

public class filereaderdemo{

public static void Main (string[] args) throws IOException

{//Create a file to read the object and specify the name of the file associated

To ensure that the file is already present, an exception will occur if it does not exist FileNotFoundException

FileReader fr=new FileReader ("Demo.txt");

Call the Read method for reading the stream object

Read (): reads one character at a time and automatically reads down

int Ch1=fr.read ();

System.out.println ("ch1=" + (char) ch1);

int Ch2=fr.read ();

System.out.println ("ch1=" + (char) CH2);

int Ch3=fr.read ();

System.out.println ("ch1=" + (char) CH3);

int Ch4=fr.read ();

System.out.println ("ch1=" + (char) CH4);

Fr.close ();

}

}

/*

* Read by a character array

*/

Import java.io.*;

public class filereaderdemo2{

public static void Main (string[] args) throws IOException

{

FileReader fr=new FileReader ("Demo.txt");

Defines an array of characters that are used to store read characters

The read (char[]) returns the number of characters

Char[] Buf=new char[3];

int num=0;

while ((Num=fr.read (BUF))!=-1)

{

System.out.println (New String (Buf,0,num));

}

Fr.close ();

}

}

Io stream text file read mode

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.