如何将Map集合写入txt文件中

public void save(String file) throws Exception {
		if(file == null) {
			throw new Exception("file is null");
		}else {
			
			Map map = addResource("C:\\test"); //此处是生成map集合
			String line = System.getProperty("line.separator");
			StringBuffer str = new StringBuffer();
			FileWriter fw = new FileWriter(file,false); //file是将要储存文件得地址,true/false是再次保存时是否覆盖上一次文件
			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();
		}
	}

你可能感兴趣的:(笔记,java)