2个回答
展开全部
读取java文件就要用到io了,每一个单词应该会有分隔符,可能是空格,逗号,
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File(""/** 文件名 */
);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String sb = "";
int count = 0;
sb = br.readLine();
while (sb != null) {
String[] array = sb.split(" "/** 文件分隔符 */
);
for (String string : array) {
if (string.equals("while")) {
count++;
}
}
sb = br.readLine();
}
System.out.println(count);
}
}
更多追问追答
追问
我还有些其它问题能不能QQ联系?
追答
709423445
2014-07-05 · 知道合伙人数码行家
关注
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class FileNumberDemo {
public static void main(String[] args) throws IOException{
File file = new File("D:\\a.txt");
if(!file.exists())
throw new RuntimeException("文件不存在!");
BufferedReader fw = new BufferedReader(new FileReader(file));
String str = null,result = "";
while((str=fw.readLine())!=null){
result+=str;
}
String[] arr = result.split(",");
HashMap<String,Integer> map = new HashMap<String,Integer>();
for(int i = 0; i < arr.length; i++){
Integer value = map.get(arr[i]);
if(value==null)
map.put(arr[i], 1);
else
map.put(arr[i], ++value);
}
Set<Map.Entry<String,Integer>> set = map.entrySet();
for(Iterator<Map.Entry<String,Integer>> it = set.iterator(); it.hasNext();){
Map.Entry<String,Integer> entry = it.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+":"+value);
}
}
}
//文件内容:
char,int
,
float,
char,
int,double
//测试结果:
char:2
int:2
double:1
float:1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询