黑马程序员技术交流社区
标题:
求问一道面试题
[打印本页]
作者:
mortonnnn
时间:
2015-7-20 21:56
标题:
求问一道面试题
在d盘目录下有一个加密文件a.txt(文件里只有英文和数字),密码是“heima”,当密码输入正确才能读取问价里的数据。
要求用代码来模拟读取文件的过程,并统计文件里各个字母出现的次数,并把统计结果按照“a:2个;b:3个”类似的格式打印
思路:没有String和数组以及对象,那么就不用PrintStream,直接用FileInputStream和BufferedReadermi即可,关于密码,就是封装一下键盘,然后判断一下就ok了
考虑到只有英文和数字,要用字符缓冲流,但。。。BufferedRead.read()???返回值是什么类型??答:byte!!!
代码:
public Test {
public static void main(String[] args){
//用scanner来记录键盘键入的字符串
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
if(str.equals("heima"){
read();
}
}
public void read(){
Byte[] bytes;
BufferedReader br = new BufferedReader(new FileReader("D:\a.txt");
while((br.read()) != -1){
bytes.add(br.read());
}
ook
做到这儿感觉做不下去了。。。求助。。。
作者:
为明天而奋斗
时间:
2015-7-20 22:05
是什么面试题啊,视屏面试题吗
作者:
mortonnnn
时间:
2015-7-20 22:08
为明天而奋斗 发表于 2015-7-20 22:05
是什么面试题啊,视屏面试题吗
不知道哎,是我同学给我的
作者:
wudigod12
时间:
2015-7-20 22:55
看看行不
package test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class test6 {
public static void main(String[] args) throws IOException{
Scanner scan = new Scanner(System.in);
String str = null;
while((str = scan.nextLine()) != null){
if("heima".equals(str)){
readFile();
break;
}else{
System.out.println("请重新输入密码");
}
}
}
public static void readFile() throws IOException{
TreeMap<Character,Integer> m = new TreeMap<Character,Integer>();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\a.txt"));
int by = 0;
while((by=bis.read()) != -1){
if(by < '0' || by > '9'){
System.out.print((char)by);
if(!m.containsKey((char)by)){
m.put((char)by, 1);
}else{
m.put((char)by, m.get((char)by) + 1);
}
}
}
Set<Entry<Character, Integer>> set = m.entrySet();
Iterator<Entry<Character, Integer>> it = set.iterator();
while(it.hasNext()){
Entry<Character,Integer> e = it.next();
System.out.print(e.getKey() + ":" + e.getValue() + "\t");
}
}
}
作者:
wudigod12
时间:
2015-7-20 23:10
再判断下(by != ' ' && by != '\r' && by != '\n')比较完善了.
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2