今天在做一个练习:统计文本中某个字符串出现的次数
刚开始用的是FileReader,方法代码如下:
public static int countstr(String filename, String str) throws FileNotFoundException, IOException { FileReader fr = new FileReader(filename); BufferedReader br = new BufferedReader(fr); StringBuilder strb = new StringBuilder(); while (true) { String line = br.readLine(); if (line == null) { break; } strb.append(line); } String result = strb.toString(); char [] arr = result.toCharArray(); int count = 0; for(int i=0;i<arr.length;i++){ String arr1 = arr[i]+""; if(str.equals(arr1)){ count ++; } } br.close(); return count; }
我在文本里随便写了几个英文字符串,发现运行没问题。后来改成一连串汉字,发现问题来了,读取的时候有乱码,于是查资料发现了解决办法代码改成如下:
public static int countstr(String filename, String str) throws FileNotFoundException, IOException { //FileReader fr = new FileReader(filename); InputStreamReader isr =new InputStreamReader(new FileInputStream(filename), "GBK"); BufferedReader br = new BufferedReader(isr); StringBuilder strb = new StringBuilder(); while (true) { String line = br.readLine(); if (line == null) { break; } strb.append(line); } String result = strb.toString(); char [] arr = result.toCharArray(); int count = 0; for(int i=0;i<arr.length;i++){ String arr1 = arr[i]+""; if(str.equals(arr1)){ count ++; } } br.close(); return count; }
之后想了解FileReader与InputStreamReader的区别,区别如下:
1.FileInputStream:以字节流方式读取;FileReader:把文件转换为字符流读入。
2.Reader类及其子类提供的字符流的读取char(16位,unicode编码),inputStream及其子类提供字节流的读取byte(8位),所以FileReader类是将文件按字符流的方式读取,FileInputStream则按字节流的方式读取文件;