为什么我的bianli方法进去就直接跳到最后一句了啊?还有为什么的主方法中s只能写具体文件名,而不能写文件夹名?一写就报错,哪位大神能教教我

qq_16977471 2018-04-27 12:32:30
/统计本项目中(所有java文件中)每个字母出现的次数(不用按照字母的顺序排列),按照如下格式打印:
// a(1000)b(2000)c(3000)d(4000)...

public class Test {
public static void main(String[] args) throws IOException {
// String s = "C:\\Users\\Administrator\\Desktop";
String s = "C:\\Users\\Administrator\\Desktop\\Test03.java";
File file = new File(s);
Map<Character, Integer> map = new HashMap<>();
bianli(file, map);
System.out.println(map);
}

//遍历文件夹
public static void bianli(File file,Map<Character, Integer> map) throws IOException {


if(file.isDirectory()){
File[] f = file.listFiles();
for (File file2 : f) {
if(file2.isFile()){
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null){
char[] ch = line.toCharArray();
for (char c : ch) {
if(c>='a'&&c<='z'){
if(!map.containsKey(c)){
map.put(c, 1);
}else{
map.put(c, map.get(c)+1);
}
}
}

}

}
else{
bianli(file2,map);
}
}
}else{
System.out.println(1);
}


}
}
...全文
1382 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿顾同学 2018-05-06
  • 打赏
  • 举报
回复
这是我写的文件夹和文件的遍历,你可以看看,我里面画了程序运行的逻辑流程图 你自己从无到有的把代码和流程画一边,你就对遍历这一块比较熟悉了 https://blog.csdn.net/u010452388/article/details/80006983
阿顾同学 2018-05-06
  • 打赏
  • 举报
回复
1.代码帮你重新改了,有的地方给你加了注释,
2.如果是文件的话,直接让他下面子目录重新递归,
3.如果是文件,我们才要去里面读数据
4.这是我自己测试的结果,你把下面代码拷贝过去,把文件路径改下就行了
5.还有有一个br读取完数据后,要记得关流



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//统计本项目中(所有java文件中)每个字母出现的次数(不用按照字母的顺序排列),按照如下格式打印:
// a(1000)b(2000)c(3000)d(4000)...

public class Test {
public static void main(String[] args) throws IOException {
// String s = "C:\\Users\\Administrator\\Desktop";
String s = "D:\\Program Files\\workspace\\test1pro\\src\\cn\\itcast\\app\\Test.java";
File file = new File(s);
Map<Character, Integer> map = new HashMap<>();
bianli(file, map);
System.out.println(map);
}

// 遍历文件夹
public static void bianli(File file, Map<Character, Integer> map) throws IOException {
if (file.isDirectory()) {// 这句话判断是不是文件夹
File[] f = file.listFiles();
// 对下面子目录再进行循环,并且递归遍历
for (File file2 : f) {
bianli(file2, map);
}
} else {// 否则是文件,这个时候我们要到文件里去读取数据,并把是字母的数据添加到Map中
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
char[] ch = line.toCharArray();
for (char c : ch) {
if (c >= 'a' && c <= 'z') {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
}
}
br.close();
}

}
}
qq_16977471 2018-04-28
  • 打赏
  • 举报
回复
改成file2还是没用,断点调试看是if语句没进去,直接到else语句了,我想不懂,我的if语句写的有问题吗
QWERT4745 2018-04-27
  • 打赏
  • 举报
回复

BufferedReader br = new BufferedReader(new FileReader(file));
这句写错了,应该是file2

BufferedReader br = new BufferedReader(new FileReader(file2));
还有路径s,可以写文件夹的名字。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧