对象的序列化异常及从文件里反序列化多个对象
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Comparator; import java.util.InputMismatchException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class LinkedListDemo implements Serializable{ private List<StuGrade> list; public List<StuGrade> getList() { return list; } public void setList(List<StuGrade> list) { this.list = list; } public LinkedListDemo() { list = new LinkedList<StuGrade>(); } Scanner sc = new Scanner(System.in); // 1.用户从界面输入学生成绩 public void InputGrade(StuGrade sg) { float a1,a2,a3,a4=0; System.out.println("姓名"); sg.setName(sc.next()); boolean flag; a1 = a2 = a3 = a4 = 0; String b = null; System.out.println("请输入相应成绩(满分为100)"); do { flag = true; try { if (a1 == 0) { System.out.print("语文成绩:"); a1 = sc.nextFloat(); } if(a1 < 0 || a1 >100) { throw new Grade1(); } } catch(Grade1 e) { System.out.println(e.getMessage()); a1 = 0; flag = false; continue; }catch (InputMismatchException e) { // TODO: handle exception System.out.println("请勿输入除0-100外的其他字符"); b = sc.next(); flag = false; continue; } try { if (a2 == 0) { System.out.print("数学成绩:"); a2 = sc.nextFloat(); } if(a2 < 0 || a2 >100) { throw new Grade1(); } } catch(Grade1 e) { System.out.println(e.getMessage()); a2 = 0; flag = false; continue; }catch (InputMismatchException e) { // TODO: handle exception System.out.println("请勿输入除0-100外的其他字符"); b = sc.next(); flag = false; continue; } try { if (a3 == 0) { System.out.print("英语成绩:"); a3 = sc.nextFloat(); } if(a3 < 0 || a3 >100) { throw new Grade1(); } } catch(Grade1 e) { System.out.println(e.getMessage()); a3 = 0; flag = false; continue; }catch (InputMismatchException e) { // TODO: handle exception System.out.println("请勿输入除0-100外的其他字符"); b = sc.next(); flag = false; continue; } try { if (a4 == 0) { System.out.print("Java成绩:"); a4 = sc.nextFloat(); } if(a4 < 0 || a4 >100) { throw new Grade1(); } } catch(Grade1 e) { System.out.println(e.getMessage()); a4 = 0; flag = false; continue; }catch (InputMismatchException e) { // TODO: handle exception System.out.println("请勿输入除0-100外的其他字符"); b = sc.next(); flag = false; continue; } } while (!flag); sg.setChinese(a1); sg.setMath(a2); sg.setEnglish(a3); sg.setJava(a4); this.getList().add(sg); } // 2.显示学生信息 public void display() { System.out.println("以下是学生成绩信息:"); Iterator<StuGrade> it = this.getList().iterator(); while (it.hasNext()) { System.out.println(it.next()); } } // 用户可以自定义设置权值 public void weighted() { System.out.println("是否更改默认权值(科目权值均为0.25)"); System.out.println("1.是"); System.out.println("2.否"); int n = sc.nextInt(); float a1, a2, a3, a4; a1 = a2 = a3 = a4 = 0.25f; switch (n) { case 2: break; case 1: boolean flag; a1 = a2 = a3 = a4 = 0; String b = null; System.out.println("请为每一项成绩设置加权值(总权值不超过1)"); do { flag = true; try { if (a1 == 0) { System.out.print("语文加权值:"); a1 = sc.nextFloat(); } } catch (InputMismatchException e) { // TODO: handle exception System.out.println("请勿输入除0-1外的其他字符"); b = sc.next(); flag = false; continue; } try { if (a2 == 0) { System.out.print("数学加权值:"); a2 = sc.nextFloat(); } } catch (InputMismatchException e) { // TODO: handle exception System.out.println("请勿输入除0-1外的其他字符"); b = sc.next(); flag = false; continue; } try { if (a3 == 0) { System.out.print("英语加权值:"); a3 = sc.nextFloat(); } } catch (InputMismatchException e) { // TODO: handle exception System.out.println("请勿输入除0-1外的其他字符"); b = sc.next(); flag = false; continue; } try { if (a4 == 0) { System.out.print("Java加权值:"); a4 = sc.nextFloat(); } } catch (InputMismatchException e) { // TODO: handle exception System.out.println("请勿输入除0-1外的其他字符"); b = sc.next(); flag = false; continue; } try { if ((a1 + a2 + a3 + a4) != 1) { throw new weighted1(); } } catch(weighted1 e) { System.out.println(e.getMessage()); a1 = a2 = a3 = a4 = 0; flag = false; continue; } catch (Exception e) { e.printStackTrace(); } } while (!flag); break; } Iterator<StuGrade> it = this.getList().iterator(); while (it.hasNext()) { StuGrade sg = it.next(); sg.setAve(a1 * sg.getChinese() + a2 * sg.getMath() + a3 * sg.getEnglish() + a4 * sg.getJava()); } } // 3.升序排列 class Ascending implements Comparator<StuGrade> { @Override public int compare(StuGrade o1, StuGrade o2) { // TODO Auto-generated method stub return new Float(o1.getAve()).compareTo(new Float(o2.getAve())); } } // 4.降序排列 class Descending implements Comparator<StuGrade> { @Override public int compare(StuGrade o1, StuGrade o2) { // TODO Auto-generated method stub return new Float(o2.getAve()).compareTo(new Float(o1.getAve())); } } // 5.导入学生成绩文件 public void InputFile() { // 读取 Scanner input = null; try { FileInputStream fis = new FileInputStream("chengji.txt"); InputStreamReader isr = new InputStreamReader(fis); input = new Scanner(fis, "GBK"); } catch (FileNotFoundException e) { // TODO: handle exception System.out.println("此文件不存在"); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } while (input.hasNext()) { for (int i = 0; i < 3; i++) { StuGrade sg = new StuGrade(); sg.setName(input.next()); sg.setChinese(input.nextFloat()); sg.setMath(input.nextFloat()); sg.setEnglish(input.nextFloat()); sg.setJava(input.nextFloat()); this.getList().add(sg); } } System.out.println("txt 内容:"); this.display(); } // 6.将学生成绩导出到ave.txt文件 public void OutputFile() { try { FileOutputStream fos = new FileOutputStream("ave.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); System.out.println(this); oos.writeObject(this); oos.flush(); fos.close(); oos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 7.将文件内容反序列化 public void InputObject() { try { FileInputStream fis = new FileInputStream("ave.txt"); ObjectInputStream ois = new ObjectInputStream(fis); // 怎么将文件里的多个对象反序列化输出?不知道怎么写循环诶 // LinkedListDemo lld1 = (LinkedListDemo) ois.readObject(); List lld1 = (LinkedList) ois.readObject(); Iterator<StuGrade> it = lld1.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } catch (Exception e) { // TODO: handle exception } System.out.println("反序列化成功!"); } }
这是我LinkedListDemo类的全部代码,刚刚输出了this 里面确实没有东西。
35
收起
正在回答
3回答
你往里写的时候是一个List的对象,读的时候还是读一个List对象。这个要统一,参看26行和39行。你看一下你的代码是不是不统一了呢。另我输入的有中文输出的还是中文这个没有问题的呀,中间的过程如ave.txt文件你不用管的。
櫻絳_
2017-09-20 13:45:17
// 7.将文件内容反序列化 public void InputObject() { try { FileInputStream fis = new FileInputStream("ave.txt"); ObjectInputStream ois = new ObjectInputStream(fis); // 怎么将文件里的多个对象反序列化输出?不知道怎么写循环诶 LinkedListDemo lld1 = (LinkedListDemo) ois.readObject(); System.out.println(lld1); Iterator<StuGrade> it = lld1.getList().iterator(); while(it.hasNext()) { System.out.println(it.next()); } } catch (Exception e) { // TODO: handle exception } System.out.println("反序列化成功!"); }
问题一:先反序列化了LinkedList 再循环输出,但只输出了“反序列化成功”几个字,中间没有任何的输出结果,也没有报错。请问我该怎么改呢?
问题二:方法6导出文件,查看了writeUTF()只能传入字符串 却不能传入对象。而且我想输出中文,应该用GBK 但是API里没有给出相应的方法。
*************************************************************************************************
上题的问题一已解决,是因为括号位置打错了,才发现,谢谢助教~
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星