新手25分求教JAVA题,在线等。编写程序从一个文本文件(包含中文)中读取

来源:百度知道 编辑:UC知道 时间:2024/05/26 01:59:13
编写程序从一个文本文件(包含中文)中读取文件的内容,要求:1)用字符流和字节流两种方式读取2)统计字符和字节的个数。

FileInputStream fis=new FileInputStream("/home/soft01/abc.txt");
byte[] b=new byte[20];
int count=0;
String re="";
while(true){
int temp=fis.read(b);
if(temp==-1)
break;
re=re+new String(b,0,temp);
count=count+temp;
}
fis.close();
System.out.println("内容为:"+re+" 字节数为:"+count);

FileInputStream fis=new FileInputStream("/home/soft01/abc.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String s="";
int i=1;
while(true){
String temp=br.readLine();
if(temp==null) break;
System.out.println(i+":"+temp);
s=s+temp;
i++;

}
br.close();//只关最外层的处理流就可以了
System.out.println("内容为:"+s+" 字符数为:"+s.length());