/*
* 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