java中将map集合写入txt文本

public static void main(String[] args)
 {
  // TODO Auto-generated method stub
  
  Map map = new HashMap();
  map.put("k1","v1");
  map.put("k2","v2");
  map.put("k3","v3");
  
  try {
   String line = System.getProperty("line.separator");
   StringBuffer str = new StringBuffer();
   FileWriter fw = new FileWriter("D:\\1.txt", true);
   Set set = map.entrySet();
   Iterator iter = set.iterator();
   while(iter.hasNext()){
    Map.Entry entry = (Map.Entry)iter.next();
    str.append(entry.getKey()+" : "+entry.getValue()).append(line);
   }
   fw.write(str.toString());
   fw.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

你可能感兴趣的:(java,IO流)