把它简化了。。这里只有求a的个数
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Test { public static void main(String[] args) { File file1=new File("E:\\java\\newFile.txt"); try { FileInputStream fi=new FileInputStream(file1); int s,aa=0 ; s=fi.read(); while(s!=-1) { char c= (char) s; if(c=='a') { aa++; } s=fi.read(); } System.out.println("newFile.txt中a的个数为:"+aa); fi.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }