如果有一个abc.txt文件
文件内容:abcd efgh I don't know
1.当其读入上述文件时,可以统计特定字母的个数。
2.这个特定的字母在创建对象时指定FileInputStream file = null;
file= new FileInputStream("abc.txt");
int len = 0;
String temp="a";
int num=0;
byte[] bt = new byte[1024];
while((len=file.read(bt,0,1024))>0) {
for(int i = 0; i < len; i++) {
if(bt[i].equals(temp)) {
num++;
}
}
System.out.plint(num);
}
这样做行吗?
文件内容:abcd efgh I don't know
1.当其读入上述文件时,可以统计特定字母的个数。
2.这个特定的字母在创建对象时指定FileInputStream file = null;
file= new FileInputStream("abc.txt");
int len = 0;
String temp="a";
int num=0;
byte[] bt = new byte[1024];
while((len=file.read(bt,0,1024))>0) {
for(int i = 0; i < len; i++) {
if(bt[i].equals(temp)) {
num++;
}
}
System.out.plint(num);
}
这样做行吗?
解决方案 »
- java OO设计问题 大家来讨论下
- 关于GridLayout的格式排版
- java文件选择问题 急!!!!!!!!!!!!!!
- 一道简单的java习题?
- 如何从WindosXP中将日志(应用程序、安全性和系统日志)提取出来,转换成EXCEL格式存储到一个数据库中
- 一个Java Applet程序的与服务器的数据交换问题
- 这两个小程序都一样,怎么一个可以编译一个却不行的!
- 在java如何得到所有本机的进程信息?
- 发电子邮件一定要用Javamail才能发吗?
- 如何用JBUILDER 7.0 建菜单,在线等待。
- 我编写的应用TreeSet进行排序的代码
- 请帮小弟把wsforandroid.wsdl文件生成(Java) web service客户端代码,不胜感激!!
FileInputStream file = null;
try {
file = new FileInputStream("d:\\abc.txt");
int len = 0;
char temp = 'a';
int num = 0;
byte[] bt = new byte[1024];
if ((len = file.read(bt, 0, 1024)) > 0) {
String s = new String(bt,0,len);
for (int i = 0; i < len; i++) {
if (s.charAt(i) == temp) {
num++;
}
}
System.out.println(num);
}
} catch (Exception e) {
e.printStackTrace();
}
}
然后再用字符串的方法去处理就可以了
public static void main(String[] args) {
BufferedReader br = null;
String line = null;
char temp = 'a';
int count = 0;
try{
br = new BufferedReader(new FileReader("d:abc.txt")); //BufferedReader带缓冲区的Reader
while((line = br.readLine())!= null) { //从文件读进一行给line
for(int i=0; i<line.length();i++) { //对line字符串的匹配
if(line.charAt(i)==temp)
count ++;
}
}
System.out.println(count);
}catch(IOException e){
e.printStackTrace();
}finally { //最终关闭资源
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
int len = 0;
//String temp="a";
char temp = 'a';
int num=0;
byte[] bt = new byte[1024];
while((len=file.read(bt,0,1024))>0) {
for(int i = 0; i < len; i++) {
//if(bt[i].equals(temp)) {
if((char)bt[i] == temp)
num++;
}
}
BufferedReader file = new BufferedReader(new FileReader("abc.txt"));
char temp = 'a';
int read = 0, num = 0;
while((read=file.read())!=-1) {
if((char)read == temp) {
num++;
}
}
System.out.plint(num);