java中map集合的持久化保存,及其读取

2024-10-20 20:28:51

1、@Testpublic void test05() throws Exception{//创建一个文件File file=new File("d.txt");//文件输出流FileOutputStream fos=new FileOutputStream(file);String str="你好";//缓存流BufferedOutputStream bos=new BufferedOutputStream(fos);fos.write(str.getBytes());fos.close();}

java中map集合的持久化保存,及其读取

3、//创建文件保存数据,从API中可以了解到FileOutputStream的构造函数有这个,true代表追加,FileOutputStrea罪焐芡拂m fos=new FileOutputStream("d.txt",true);//文件的序列化ObjectOutputStream oos=new ObjectOutputStream(fos);//writeObject 方法用于将对象写入流中。所有对象(包括 String 和数组)都//可以通过 writeObject 写入。oos.writeObject(maps);oos.close();}

java中map集合的持久化保存,及其读取

5、//map集合的遍历,有三种方式,此为其中一种Set<Map.Entry<Integer,String>>sets=maps.entrySet();for(Map.Entry<Integer, String> s:sets){System.out.println("key="+s.getKey()+"value="+s.getValue());}}

java中map集合的持久化保存,及其读取
猜你喜欢