java中将list、map对象写入文件

链接地址:http://blog.sina.com.cn/s/blog_4a4f9fb50101p6jv.html

 
http://d1.sina.com.cn/pfpghc2/201512/18/dcd35f0abd134262b4a1d66dd1e3c45e.jpg

http://d1.sina.com.cn/pfpghc2/201601/08/a2740a5311494f2bab1670b01e5706b9.jpg

 
个人资料
doudouhe
doudouhe
 
微博
 
 
 

加好友发纸条

写留言加关注

 
 
博客十周年
博客十周年地图
  • 博客等级:
  • 博客积分:842积分
  • 博客访问:222,144
  • 关注人气:34
  • 获赠金笔:14
  • 赠出金笔:0
  • 荣誉徽章:点亮荣誉勋章兑换图片博主服务兑换私密博文兑换金笔
 
 
 
http://d1.sina.com.cn/201410/24/578319_210x220.jpg
http://d5.sina.com.cn/pfpghc2/201601/04/77ab571c408345ccab751646679fdb52.jpg
谁看过这篇博文
 
正文字体大小:  

java中将list、map对象写入文件

 (2014-03-17 17:39:36)
标签: 

it

分类: JAVA
java的IO包当中提供了,向文件中写入文件和读取文件的方法。好吧,来看下具体怎么回事吧。我们先写一个像文件当中写单个对象的方法吧。 

public void writeObject() {
try {

HashMap<</span>String,String> map = new HashMap<</span>String,String>();
map.put("name", "foolfish");


FileOutputStream outStream = new FileOutputStream("E:/1.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);

objectOutputStream.writeObject(map);
outStream.close();
System.out.println("successful");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

这里我们将一个map对象插入一个txt文件当中。java的IO包当中提供了Object的文件流。代码很简单,我们下面来看一看从该文件当中读取这个对象吧 

public void readObject(){
FileInputStream freader;
try {
freader = new FileInputStream("E:/1.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(freader);
HashMap<</span>String,String> map = new HashMap<</span>String,String>();
map = (HashMap<</span>String, String>) objectInputStream.readObject();

System.out.println("The name is " + map.get("name"));

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

代码也很简单,我们使用ObjectInputStream 的readobject的就可以读取文件中的对象,再按照封装对

象时候的类型进行强制转换一下。输出结果是aa foolfish。

       上面提供的是对单个对象的存入和读取。对多个不同的对象该方法也适用。还是用代码来说明下吧。我们同时插入两个不同的对象,一个map,一个list。


public class ObjectToFile {
public void writeObject() {
try {

HashMap<</span>String,String> map = new HashMap<</span>String,String>();
map.put("name", "foolfish");

List<</span>String> list = new ArrayList<</span>String>();
list.add("hello");
list.add("everyone");


FileOutputStream outStream = new FileOutputStream("E:/1.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);

objectOutputStream.writeObject(map);
objectOutputStream.writeObject(list);
outStream.close();
System.out.println("successful");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void readObject(){
FileInputStream freader;
try {
freader = new FileInputStream("E:/1.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(freader);
HashMap<</span>String,String> map = new HashMap<</span>String,String>();
map = (HashMap<</span>String, String>) objectInputStream.readObject();
ArrayList<</span>String> list = new ArrayList<</span>String>();
list = (ArrayList<</span>String>) objectInputStream.readObject();
System.out.println("The name is " + map.get("name"));
System.out.println("aa " + list.get(1));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


public static void main(String args[]){
ObjectToFile of = new ObjectToFile();
of.writeObject();
of.readObject();
}
}
posted @ 2016-01-11 10:49  *新星月-倪新生*  阅读(2295)  评论(0编辑  收藏  举报