StringBuffer buf = new StringBuffer("");
      for(Iterator it = has.keySet().iterator();it.hasNext();) {
         String key = (String)it.next();
         buf.append(key+"|"+has.get(key));
      }
      // write the buf to file //     BufferedReader in;
      while(in.ready()) {
         String str = in.readLine();
         StringTokenizer tok = new StringTokenizer(str,"|");
         String key = tok.nextToken();
         String value = tok.nextToken();
         has.put(key,value);
      }

解决方案 »

  1.   

    写入文件:
    public static void main(String[] args) {
        Map has = new HashMap();
        has.put("word", "1");
        has.put("love", "2");
        try {
          PrintWriter writer = new PrintWriter(new FileOutputStream("c:/myword.txt"), true);
          Iterator i = has.keySet().iterator();
          while (i.hasNext()) {
            String word = i.next().toString();
            int num = Integer.parseInt(has.get(word).toString());
            writer.println(word + "|" + num);
          }
        }
        catch (Exception e) {
        }
      }
      

  2.   

    从文件导入has中:
    public static void main(String[] args) {
        Map has = new HashMap();
        try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(new
              FileInputStream("c:/myword.txt")));
          String info = reader.readLine();
          while (info != null) {
            String[] a = info.split(",");
            String word = a[0];
            String num = a[1];
            has.put(word, num);
            info = reader.readLine();
          }
        }
        catch (Exception e) {
        }
      }
      

  3.   

    更正:
    写入文件的那一段中的
    writer.println(word + "|" + num);
    这一句中的"|"改为","
    否则导入则会出错!
      

  4.   

    to:satangf(好好学习,天天向上!)int num = Integer.parseInt(has.get(word).toString());为什么has.get(word)得出来总是null?
      

  5.   

    String word = i.next().toString();
            int num = Integer.parseInt(has.get(word).toString());
    改为
            Object word = i.next();
            int num = Integer.parseInt(has.get(word).toString());
    搞定!谢谢二位