求各位大牛来解决这道题,小弟不胜感激~~~~~·
1. 读取文件内容,并打印,统计每个字符出现的个数并打印,按字母升序排列,输出到另一文件中。
2. 用junit测试上述代码面试用友javaseJUnit

解决方案 »

  1.   

    package com.interview.yongyou;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;/**
     * 1. 读取文件内容,并打印,统计每个字符出现的个数并打印,按字母升序排列,输出到另一文件中。
     * 2. 用junit测试上述代码
     * @author 背道而驰
     */
    public class CharSortIO {
    public static void main(String[] args){
    CharSortIO charSortIO=new CharSortIO();
    String filePath="d:/a.txt";//初始文件的内容
    String value=charSortIO.readFile(filePath);//读取文件内容
    charSortIO.printCharNumber(value);//打印字符出现数量
    String targetPath="d:/b.txt";//写文件的路径
    charSortIO.writeFile(targetPath, value);//排序后将其写入到文件中去
    }

    /**
     * function:根据路径读取文件内容
     */
    public String readFile(String filePath){
    StringBuffer tempBuffer=new StringBuffer("");
    try{
    File file=new File(filePath);
    BufferedReader br=new BufferedReader(new FileReader(file));
    String tempStr=null;
    while((tempStr=br.readLine())!=null){
    tempBuffer.append(tempStr);
    }
    br.close();
    }catch(Exception e){
    throw new RuntimeException("文件读取失败了...");
    }
    return tempBuffer.toString();
    }

    /**
     * 统计每个字符出现的次数
     */
    public void printCharNumber(String value){
    Map<Character,Integer> charNumber=new HashMap<Character,Integer>();
    int size=value.length();
    for(int i=0;i<size;i++){
    Character ch=value.charAt(i);
    if(ch!=32){//不统计空格
    if(charNumber.containsKey(ch)){
    charNumber.put(ch, charNumber.get(ch)+1);
    }else{
    charNumber.put(ch, 1);
    }
    }
    }

    //然后打印对应的Map
     Iterator it=charNumber.entrySet().iterator();
     while(it.hasNext()){
     System.out.println(it.next());
     }
    }

    /**
     * 按字母升序排列并写到b.txt中去
     */
    public void writeFile(String targetPath,String value){
    try{
    char[] chars=value.toCharArray();
    sort(chars);
    StringBuffer fileStr=new StringBuffer("");
    for(int i=0;i<chars.length;i++){
    fileStr.append(chars[i]);
    }
    File file=new File(targetPath);
    if(!file.exists()){
    file.createNewFile();
    }
    FileWriter fw=new FileWriter(file);
    fw.write(fileStr.toString());
    fw.flush();
    fw.close();
    }catch(Exception e){
    throw new RuntimeException("写文件操作失败了...");
    }

    }

    /**
     * function:对字符数组进行排序
     */
    public static void sort(char[] chars){
    int size=chars.length;
    for(int i=0;i<size;i++){
    for(int j=i;j<size;j++){
    if(chars[i]>chars[j]){
    char temp;
    temp=chars[i];
    chars[i]=chars[j];
    chars[j]=temp;
    }
    }
    }
    }
    }
      

  2.   

    由于写了main方法,就不写junit测试了。。
      

  3.   


    package com.interview.yongyou;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;/**
     * 1. 读取文件内容,并打印,统计每个字符出现的个数并打印,按字母升序排列,输出到另一文件中。
     * 2. 用junit测试上述代码
     * @author 背道而驰
     */
    public class CharSortIO {
    public static void main(String[] args){
    CharSortIO charSortIO=new CharSortIO();
    String filePath="d:/a.txt";//初始文件的内容
    String value=charSortIO.readFile(filePath);//读取文件内容
    charSortIO.printCharNumber(value);//打印字符出现数量
    String targetPath="d:/b.txt";//写文件的路径
    charSortIO.writeFile(targetPath, value);//排序后将其写入到文件中去
    }

    /**
     * function:根据路径读取文件内容
     */
    public String readFile(String filePath){
    StringBuffer tempBuffer=new StringBuffer("");
    try{
    File file=new File(filePath);
    BufferedReader br=new BufferedReader(new FileReader(file));
    String tempStr=null;
    while((tempStr=br.readLine())!=null){
    tempBuffer.append(tempStr);
    }
    br.close();
    }catch(Exception e){
    throw new RuntimeException("文件读取失败了...");
    }
    return tempBuffer.toString();
    }

    /**
     * 统计每个字符出现的次数
     */
    public void printCharNumber(String value){
    Map<Character,Integer> charNumber=new HashMap<Character,Integer>();
    int size=value.length();
    for(int i=0;i<size;i++){
    Character ch=value.charAt(i);
    if(ch!=32){//不统计空格
    if(charNumber.containsKey(ch)){
    charNumber.put(ch, charNumber.get(ch)+1);
    }else{
    charNumber.put(ch, 1);
    }
    }
    }

    //然后打印对应的Map
     Iterator it=charNumber.entrySet().iterator();
     while(it.hasNext()){
     System.out.println(it.next());
     }
    }

    /**
     * 按字母升序排列并写到b.txt中去
     */
    public void writeFile(String targetPath,String value){
    try{
    char[] chars=value.toCharArray();
    sort(chars);
    StringBuffer fileStr=new StringBuffer("");
    for(int i=0;i<chars.length;i++){
    fileStr.append(chars[i]);
    }
    File file=new File(targetPath);
    if(!file.exists()){
    file.createNewFile();
    }
    FileWriter fw=new FileWriter(file);
    fw.write(fileStr.toString());
    fw.flush();
    fw.close();
    }catch(Exception e){
    throw new RuntimeException("写文件操作失败了...");
    }

    }

    /**
     * function:对字符数组进行排序
     */
    public static void sort(char[] chars){
    int size=chars.length;
    for(int i=0;i<size;i++){
    for(int j=i;j<size;j++){
    if(chars[i]>chars[j]){
    char temp;
    temp=chars[i];
    chars[i]=chars[j];
    chars[j]=temp;
    }
    }
    }
    }
    }
      

  4.   

    package com.yyj;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;public class ReadFile {
        /**
         *<p>
         *description:
         *</p>
         *@param args
         * @author ex_yuanyingjie
         * @see
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub  z 122  a 97  A 65 Z  90
            Map<Character,Integer> map = new ReadFile().read("目录.txt");
            for(char key : map.keySet()){
                System.out.println(key+":"+map.get(key));
            }
        }
        public Map<Character,Integer> read(String path){
            File file = new File(path);
            FileInputStream fin = null;
            Map<Character,Integer> map = new HashMap<Character,Integer>();
            try {
                fin = new FileInputStream(file);
                int info = -1;
                while((info=fin.read())!=-1){
                    if((info>=65&&info<=90)||(info>=97&&info<=122)){
                        if(map.containsKey((char)info)){
                           map.put((char)info,  map.get((char)info)+1);
                        }else{
                            map.put((char)info, 0);
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return map;
        }
    }
      

  5.   

    不用工具,不看API我是写不出来···· 
    自叹不如···
      

  6.   

    这样也行import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.Map;
    import java.util.TreeMap;public class Toto {
        
        private Map<Character, Integer> out = new TreeMap<Character, Integer>();
        
        private StringBuffer in = new StringBuffer();
        
        public Toto(FileReader fr) throws IOException{
            BufferedReader br = null;
            br = new BufferedReader(fr);
            int ci;
                while((ci = br.read()) != -1){
                    in.append((char)ci);
                    if (out.get((char)ci) == null){
                        out.put((char)ci, 1);
                    }
                    else {
                        out.put((char)ci, out.get((char)ci)+1);
                    }
                }
        }
        
        public void printIn(PrintStream os){
            os.print(in);
        }
        
        public void printOut(PrintStream os){
            for(Character c : out.keySet()){
                os.println(c+":"+out.get(c));
            }
        }
        
        public static void main(String... args) throws IOException {
            if (args == null || args.length != 2){
                System.exit(1);
            }
            
            Toto t = new Toto(new FileReader(new File(args[0])));
            t.printIn(System.out);
            t.printOut(new PrintStream(args[1]));
        }
    }
      

  7.   

    如果面试官考你“API的熟悉程度”,则调用TreeMap来做;如果面试官考你“算法设计能力”,则用“BST的插入”来做;但如果你觉得这两种方法都会的话,则把他们都写上去。就像“把数组排序”这个面试题一样,你把Arrays.sort()和冒泡排序都写上去,不就行了?
      

  8.   


    这道题的面试目的主要是测试应聘者的动手能力和测试能力~
    那就果断调用TreeMap。
      

  9.   

    import java.io.*;
    import java.util.*;public class ReaderCount 
    {
    public static void main(String[] args) throws IOException
    {
    BufferedReader bufr = 
    new BufferedReader(new FileReader("D:\\count_1.txt"));

    PrintWriter out = new PrintWriter(new FileWriter("D:\\count_2.txt"),true);

    String line = null;

    int count = 1;
    Map<Character,Integer> map = new TreeMap<Character,Integer>();
    TreeSet<String> set = new TreeSet<String>();

    while((line=bufr.readLine())!=null)
    {
    //打印文件
     System.out.println(line);
    set.add(line);

    char[] arr = line.toCharArray();  for(char s : arr)
     {
     out.println(s);
     }

    //统计字符
    for(int x=0; x<arr.length; x++)
    {
    if(!(arr[x]>'a' && arr[x]<'z' || arr[x]>'A' && arr[x]<'Z'))
    continue;

    if(!(map.containsKey(arr[x])))
    map.put(arr[x], count);
    else
    count = map.get(arr[x]) + 1;
    map.put(arr[x], count);

    count = 1;
    }
    }
    //写出去
    for(String str : set)
    {
    out.write(str);
    out.println();
    }

    StringBuilder sb = new StringBuilder();

    Iterator<Character> it = map.keySet().iterator();

    while(it.hasNext())
    {
    char key = it.next();
    int value = map.get(key);

    sb.append(key+"("+value+")");
    }
    System.out.println("-----------统计字符个数------------");
    System.out.println(sb.toString());

    bufr.close();
    out.close();
    }
    }