请教:如何将HashMap中的内容写入到文件?

a673921712 2013-02-01 03:56:45

题目:完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部的信息保存在文件中,
可以实现对学生信息的全面查找,输出全部学生信息的功能。
*/
import java.util.*;
import java.io.*;
class Student implements Serializable{
int num;
String name;
double scores[]; //三门课程成绩
double aver; //平均成绩

public Student(){
this.scores=new double[3];
}
public Student(int num,String name,double score1,double score2,double score3){
this ();
this.setNumber(num);
this.setName(name);
this.setScores(score1,score2,score3);
this.aver=(this.scores[0]+this.scores[1]+this.scores[2])/3;
}

public void setNumber(int num){
this.num=num;
}
public void setName(String name){
this.name=name;
}
public void setScores(double score1,double score2,double score3){
this.scores[0]=score1;
this.scores[1]=score2;
this.scores[2]=score3;
}
public int getNumber(){
return num;
}
public String getName(){
return name;
}
public double[] getScores(){
return scores;
}
public double getAver(){
return aver;
}
public String toString(){
return "学号:"+this.num+"姓名:"+this.name+"\n"+"成绩————>"+"语文:"+this.scores[0]+"数学:"+this.scores[1]+"英语:"+this.scores[2]+"平均成绩:"+this.aver;
}
}

/*
Scanner sc=new Scanner(System.in);
sc.next()即为输入的内容;
*/
public class Test_12{
public static void main(String...args){
HashMap<Integer,Student> stu=new HashMap<Integer,Student>();
File file=new File("D:\\sb\\Stu_info.data");
Student zhangsan=new Student(18,"张三",78,85,92);
Student lisi=new Student(15,"李四",85,87,74);
Student wangwu=new Student(20,"王五",75,98,96);
Student zhaoliu=new Student(9,"赵六",93,98,93);
stu.put(18,zhangsan);
stu.put(15,lisi);
stu.put(20,wangwu);
stu.put(9,zhaoliu);
}
//写到这一步不知道该怎么写了?
}


//怎么将HashMap中的内容保存到文件,又怎么读取文件?希望各位朋友给点意见或思路.
...全文
3139 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangs0620 2013-02-02
  • 打赏
  • 举报
回复
引用 11 楼 a673921712 的回复:
引用 10 楼 liangs0620 的回复:引用 9 楼 a673921712 的回复:引用 5 楼 liangs0620 的回复:自己看吧 不懂再说 Java code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354……
把得到的hashmap作为属性封装到一个对象中再进行实例化保存,在强制类型转换的时候就不会报unchecked警告了,代码如下
package test;

//题目:完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部的信息保存在文件中,
//可以实现对学生信息的全面查找,输出全部学生信息的功能。
//
import java.util.*;
import java.io.*;

class Student implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int num;
	String name;
	double scores[]; // 三门课程成绩
	double aver; // 平均成绩

	public Student() {
		this.scores = new double[3];
	}

	public Student(int num, String name, double score1, double score2,
			double score3) {
		this();
		this.setNumber(num);
		this.setName(name);
		this.setScores(score1, score2, score3);
		this.aver = (this.scores[0] + this.scores[1] + this.scores[2]) / 3;
	}

	public void setNumber(int num) {
		this.num = num;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setScores(double score1, double score2, double score3) {
		this.scores[0] = score1;
		this.scores[1] = score2;
		this.scores[2] = score3;
	}

	public int getNumber() {
		return num;
	}

	public String getName() {
		return name;
	}

	public double[] getScores() {
		return scores;
	}

	public double getAver() {
		return aver;
	}

	public String toString() {
		return "学号:" + this.num + "姓名:" + this.name + "\n" + "成绩————>" + "语文:"
				+ this.scores[0] + "数学:" + this.scores[1] + "英语:"
				+ this.scores[2] + "平均成绩:" + this.aver;
	}
}

/*
 * Scanner sc=new Scanner(System.in); sc.next()即为输入的内容;
 */
public class Test_12 {
	public static void main(String[] args) throws Exception {
		HashMap<Integer, Student> stu = new HashMap<Integer, Student>();
		File file = new File("Stu_info.obj");
		Student zhangsan = new Student(18, "张三", 78, 85, 92);
		Student lisi = new Student(15, "李四", 85, 87, 74);
		Student wangwu = new Student(20, "王五", 75, 98, 96);
		Student zhaoliu = new Student(9, "赵六", 93, 98, 93);
		stu.put(18, zhangsan);
		stu.put(15, lisi);
		stu.put(20, wangwu);
		stu.put(9, zhaoliu);

		// 写
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		fos = new FileOutputStream(file);
		oos = new ObjectOutputStream(fos);
		oos.writeObject(new Inner(stu));
		oos.flush();
		oos.close();

		// 读
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		fis = new FileInputStream(file);
		ois = new ObjectInputStream(fis);
		Inner inner = null;
		HashMap<Integer, Student> stuRead = null;
		try {
			inner = (Inner) ois.readObject();// !!!!!!
		} catch (EOFException e) {
			e.printStackTrace();
		}finally{
			ois.close();
		}
		stuRead = inner.stu;

		// 测试
		for (Map.Entry<Integer, Student> item : stuRead.entrySet()) {
			System.out.println(item.getValue());
		}
	}
}

class Inner implements Serializable {
	/**
	 * 这个类的初衷是想将前面获得的HashMap作为对象属性进行序列化,因为如果直接将Hashmap序列化的话
	 * 在读取文件的时候(readobject),进行强制类型转换时,涉及到泛型,会报unchecked警告··· 仅此而已··
	 */
	private static final long serialVersionUID = 2L;
	HashMap<Integer, Student> stu;

	public Inner(HashMap<Integer, Student> stu) {
		this.stu = stu;
	}
}

liangs0620 2013-02-01
  • 打赏
  • 举报
回复
引用 11 楼 a673921712 的回复:
引用 10 楼 liangs0620 的回复:引用 9 楼 a673921712 的回复:引用 5 楼 liangs0620 的回复:自己看吧 不懂再说 Java code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354……
因为涉及到泛型,什么instanceOf等等方法都用不了,暂时想不到什么办法···我觉得这个警告可以无视,反正知道取出来的肯定是HashMap,也不需要为了好看添加@SuppressWarnings
a673921712 2013-02-01
  • 打赏
  • 举报
回复
引用 10 楼 liangs0620 的回复:
引用 9 楼 a673921712 的回复:引用 5 楼 liangs0620 的回复:自己看吧 不懂再说 Java code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656……
除了@SuppressWarnings,还有没有别的方法?
liangs0620 2013-02-01
  • 打赏
  • 举报
回复
引用 9 楼 a673921712 的回复:
引用 5 楼 liangs0620 的回复:自己看吧 不懂再说 Java code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767……
在强制类型转换的时候··· stuRead = (HashMap<Integer, Student>) ois.readObject();
a673921712 2013-02-01
  • 打赏
  • 举报
回复
引用 5 楼 liangs0620 的回复:
自己看吧 不懂再说 Java code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878……
@SuppressWarnings("unchecked") 程序里的集合容器都加泛型了,怎么还有这个"unchecked"提示,还有哪里没加吗?
a673921712 2013-02-01
  • 打赏
  • 举报
回复
感谢几位的回答。
致追风 2013-02-01
  • 打赏
  • 举报
回复
用BufferedWriter试试。我改了一下你的代码,可以输出文件。 import java.io.File; import java.io.IOException; import java.util.HashMap; public class Test_12 { public static void main(String[] args) throws IOException { java.io.BufferedWriter bufferWriter = null; File file =null; try { file=new File("D:\\sb\\Stu_info.data"); HashMap<Integer, Student> stu = new HashMap<Integer, Student>(); Student zhangsan = new Student(18, "张三", 78, 85, 92); Student lisi = new Student(15, "李四", 85, 87, 74); Student wangwu = new Student(20, "王五", 75, 98, 96); Student zhaoliu = new Student(9, "赵六", 93, 98, 93); stu.put(18, zhangsan); stu.put(15, lisi); stu.put(20, wangwu); stu.put(9, zhaoliu); bufferWriter = new java.io.BufferedWriter(new java.io.FileWriter(file)); bufferWriter.write(stu.get(18).toString()); bufferWriter.write(stu.get(15).toString()); bufferWriter.write(stu.get(20).toString()); bufferWriter.write(stu.get(9).toString()); bufferWriter.flush(); } finally { if (bufferWriter != null) { try { bufferWriter.close(); } catch (Exception e) { } } } } }
菖蒲老先生 2013-02-01
  • 打赏
  • 举报
回复
引用 3 楼 a673921712 的回复:
来人撒,别沉下去了。

public class Test_12{
    public static void main(String... args) throws Exception {
		HashMap<Integer, Student> stu = new HashMap<Integer, Student>();
		File file = new File("D:\\sb\\Stu_info.data");
		Student zhangsan = new Student(18, "张三", 78, 85, 92);
		Student lisi = new Student(15, "李四", 85, 87, 74);
		Student wangwu = new Student(20, "王五", 75, 98, 96);
		Student zhaoliu = new Student(9, "赵六", 93, 98, 93);
		stu.put(18, zhangsan);
		stu.put(15, lisi);
		stu.put(20, wangwu);
		stu.put(9, zhaoliu);

		write(stu);
		read();
	}

	// 写到这一步不知道该怎么写了?
	private static void write(Map<Integer, Student> map) throws Exception {
		PrintWriter pw = new PrintWriter("D:\\Stu_info.data", "utf-8");
		StringBuilder sb = new StringBuilder();
		for (Student s : map.values()) {
			sb.append(s.getNumber() + "\t");
			sb.append(s.getName() + "\t");
			sb.append(s.getScores()[0] + "\t");
			sb.append(s.getScores()[1] + "\t");
			sb.append(s.getScores()[2] + "\n");
		}
		pw.write(sb.toString());
		pw.close();
	}

	private static void read() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\Stu_info.data"), "utf-8"));
		String line = null;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
		}
	}
}
liangs0620 2013-02-01
  • 打赏
  • 举报
回复
自己看吧 不懂再说
package test;

//题目:完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部的信息保存在文件中,
//可以实现对学生信息的全面查找,输出全部学生信息的功能。
//
import java.util.*;
import java.io.*;

class Student implements Serializable {
	int num;
	String name;
	double scores[]; // 三门课程成绩
	double aver; // 平均成绩

	public Student() {
		this.scores = new double[3];
	}

	public Student(int num, String name, double score1, double score2,
			double score3) {
		this();
		this.setNumber(num);
		this.setName(name);
		this.setScores(score1, score2, score3);
		this.aver = (this.scores[0] + this.scores[1] + this.scores[2]) / 3;
	}

	public void setNumber(int num) {
		this.num = num;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setScores(double score1, double score2, double score3) {
		this.scores[0] = score1;
		this.scores[1] = score2;
		this.scores[2] = score3;
	}

	public int getNumber() {
		return num;
	}

	public String getName() {
		return name;
	}

	public double[] getScores() {
		return scores;
	}

	public double getAver() {
		return aver;
	}

	public String toString() {
		return "学号:" + this.num + "姓名:" + this.name + "\n" + "成绩————>" + "语文:"
				+ this.scores[0] + "数学:" + this.scores[1] + "英语:"
				+ this.scores[2] + "平均成绩:" + this.aver;
	}
}

/*
 * Scanner sc=new Scanner(System.in); sc.next()即为输入的内容;
 */
public class Test_12 {
	@SuppressWarnings("unchecked")
	public static void main(String... args) throws Exception {
		HashMap<Integer, Student> stu = new HashMap<Integer, Student>();
		File file = new File("Stu_info.obj");
		Student zhangsan = new Student(18, "张三", 78, 85, 92);
		Student lisi = new Student(15, "李四", 85, 87, 74);
		Student wangwu = new Student(20, "王五", 75, 98, 96);
		Student zhaoliu = new Student(9, "赵六", 93, 98, 93);
		stu.put(18, zhangsan);
		stu.put(15, lisi);
		stu.put(20, wangwu);
		stu.put(9, zhaoliu);

		// 写
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		fos = new FileOutputStream(file);
		oos = new ObjectOutputStream(fos);
		oos.writeObject(stu);
		oos.flush();
		oos.close();

		// 读
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		fis = new FileInputStream(file);
		ois = new ObjectInputStream(fis);
		HashMap<Integer, Student> stuRead = new HashMap<Integer, Student>();
		stuRead = (HashMap<Integer, Student>) ois.readObject();
		ois.close();
		// 测试
		for (Map.Entry<Integer, Student> item : stuRead.entrySet()) {
			System.out.println(item.getValue());
		}
	}
}

// 怎么将HashMap中的内容保存到文件,又怎么读取文件?希望各位朋友给点意见或思路.
nmyangym 2013-02-01
  • 打赏
  • 举报
回复
1 可以写成Object流 2 写成文本文件。(这个简单,就把对象写入文件,因为里面有toString了)。
a673921712 2013-02-01
  • 打赏
  • 举报
回复
来人撒,别沉下去了。
a673921712 2013-02-01
  • 打赏
  • 举报
回复
引用 1 楼 forgetsam 的回复:
看你要什么格式的,流读写就可以。
怎么写?给点参考,Map中的元素是一对键值,不是字符串。
forgetsam 2013-02-01
  • 打赏
  • 举报
回复
看你要什么格式的,流读写就可以。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧