黑马程序员技术交流社区
标题:
面试的题目分享
[打印本页]
作者:
Rain2692
时间:
2014-12-13 20:43
标题:
面试的题目分享
把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:
a: 21 次
b: 15 次
c: 15 次
把: 7 次
当: 9 次
前: 3 次
,:30 次
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
BufferedReader bufr = new BufferedReader(new FileReader("d:\\a.txt"));
BufferedWriter bufw = new BufferedWriter(new FileWriter("d:\\new.txt"));
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
String line = null;
while((line= bufr.readLine())!=null)
{
//这部分代码是拷贝文件
bufw.write(line);
bufw.newLine();
bufw.flush();
//以下是统计每个出现的字符数
char[] arr = line.toCharArray();
int i=0;
while(i<arr.length)
{
if(!tm.containsKey(arr[i]))//判断是否包含该字符
tm.put(arr[i],1); //不存在,则置1
else
{ //存在则取出统计的数,并操作
int count = tm.get(arr[i]);
count++;
tm.put(arr[i],count);//这点注意直接覆盖了,因为值是相同的
}
i++;
}
}
BufferedWriter bufwr = new BufferedWriter(new OutputStreamWriter(System.out));
Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();
Iterator<Map.Entry<Character, Integer>> it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Character, Integer> me = it.next();
char key = me.getKey();
int num = me.getValue();
bufwr.write(key+":"+num+"次");
bufwr.newLine();
bufwr.flush();
}
}
复制代码
作者:
马个了黑_邓
时间:
2014-12-13 20:43
哥们哪期?
作者:
Rain2692
时间:
2014-12-13 20:44
先占一个。。。
作者:
被风扬起的沙
时间:
2014-12-13 21:17
现在还看不懂,但是坚信以后会懂得
作者:
wez924612
时间:
2014-12-13 21:26
受用了!准备去面试了。
作者:
魏文杰
时间:
2014-12-13 21:37
看不懂 。。。。
作者:
Honelyboy
时间:
2014-12-13 22:06
看不懂,等学到这儿了再来。
作者:
何桥
时间:
2014-12-13 22:52
楼主可以啊
作者:
d7142947
时间:
2014-12-13 22:55
支持 感谢 分享
作者:
zhouli_2014
时间:
2014-12-13 23:12
感谢 分享
作者:
红楼
时间:
2014-12-13 23:22
膜拜。。。。。。。。。。。
作者:
ben_vs_cong
时间:
2014-12-13 23:26
谢谢分享
作者:
黑马-李权88
时间:
2014-12-13 23:26
感谢前辈分享,谢谢咯,别删帖哦
作者:
zzn2508
时间:
2014-12-13 23:58
感谢分享
作者:
木乃伊
时间:
2014-12-14 00:38
哥们牛逼:sleepy:
作者:
0_TNT_0
时间:
2014-12-14 01:18
厉害啊,膜拜
作者:
默默丶
时间:
2014-12-14 08:52
不关流吗
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2