import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//通过字符数组读取
public class FileReader2 {
public static void main(String[] args) {
FileReader fr=null;
try {
fr = new FileReader("G:\\demo.txt");
//定义一个字符数组,用于存储读到字符
char[] buf=new char[1024];
/*//fr.read(buf)把读出的字符存到数组,返回的是读到的字符个数
int num=fr.read(buf);
System.out.println(num+" "+new String(buf));*/
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));//从0开始取,取num个
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.FileReader;
import java.io.IOException;
//通过字符数组读取
public class FileReader2 {
public static void main(String[] args) {
FileReader fr=null;
try {
fr = new FileReader("G:\\demo.txt");
//定义一个字符数组,用于存储读到字符
char[] buf=new char[1024];
/*//fr.read(buf)把读出的字符存到数组,返回的是读到的字符个数
int num=fr.read(buf);
System.out.println(num+" "+new String(buf));*/
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));//从0开始取,取num个
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}