Import java.io.*;
Class Filereaderdemo
{public
static void Main (string[] args) throws IOException
{
//create a file read stream object , associated with a file with the specified name.
//To ensure that the file is already present, if it does not exist, an exception
will occur FileNotFoundException
FileReader fr = new FileReader ("Demo.txt");
Invokes the Read method that reads the stream object.
//read (): read one character at a time. And it will read down automatically.
int ch = 0;
while ((Ch=fr.read ())!=-1)
{
System.out.println ((char) ch);
}
/* While
(true)
{
int ch = fr.read ();
if (ch==-1) break
;
System.out.println ("ch=" + (char) ch);
}
*
/Fr.close ();
}
The second way: read by a character array.
Import java.io.*;
Class FileReaderDemo2
{public
static void Main (string[] args) throws IOException
{
FileReader fr = new F Ilereader ("Demo.txt");
Defines an array of characters. Used to store read-to characters.
//This read (char[]) returns the number of characters read.
char[] buf = new char[1024];
int num = 0;
while ((Num=fr.read (BUF))!=-1)
{
System.out.println (new String (Buf,0,num));
Fr.close ();
}
———— from "Bi Xiangdong 25 Days"