如何将HashMap中的数据存入txt文件,然后进行读取
怎样将HashMap中的数据存入txt文件,然后进行读取呢
HashMap里存放的是String类型和Point类型
是不是要用到正则表达式?不吝赐教,希望有简单的程序给予讲解 谢谢
[解决办法]
获得你要存储的值 用流实现就行吧
[解决办法]
既然你要存取Point类型的对象,那就序列化好了
[解决办法]
序列化
[解决办法]
写了个简单得代码
import java.io.*;import java.util.*;import java.awt.*;public class SavePoint { public static void main(String[] args) { String s = "one"; Point p = new Point(66,88); HashMap<String,Point> hm = new HashMap<String,Point>(); hm.put(s,p); //向容器中放对象 try{ ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:/ test.txt")); oos.writeObject(hm.get(s)); //从容器中取数据并输出到文件中 oos.flush(); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:/ test.txt")); Point rp = (Point)ois.readObject(); //从文件中读出数据 System.out.println("" + rp.getX() + " " + rp.getY()); ois.close(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } catch(ClassNotFoundException e) { e.printStackTrace(); } }}