首页 > 代码库 > ObjectInputStream (2)
ObjectInputStream (2)
1 package Aug14.IO; 2 3 import java.io.*; 4 import java.util.*; 5 import java.util.Map.Entry; 6 7 public class TestCurrentAccount { 8 9 private void objectWriter(Map<Integer, CurrentAccount> m, File f) { 10 try { 11 // File f = new File("D:\\Account.txt"); 12 ObjectOutputStream oos = new ObjectOutputStream( 13 new FileOutputStream(f)); 14 oos.writeObject(m); 15 oos.flush(); 16 oos.close(); 17 System.out.println("File Writing Success"); 18 } catch (Exception e) { 19 e.printStackTrace(); 20 } 21 } 22 23 @SuppressWarnings("unchecked") 24 private void objectReader(File f) { 25 try { 26 27 ObjectInputStream ois = new ObjectInputStream( 28 new FileInputStream(f)); 29 30 Map<Integer, CurrentAccount> m = (Map<Integer, CurrentAccount>) ois 31 .readObject(); 32 33 Set view = m.entrySet(); 34 Iterator it = view.iterator(); 35 while (it.hasNext()) { 36 Entry<Integer, CurrentAccount> ca = (Entry<Integer, CurrentAccount>) it 37 .next(); 38 CurrentAccount cur = ca.getValue(); 39 System.out.println("Key:- " + ca.getKey() + "\tValues:- " 40 + cur.getAccountId() + " " + cur.getName() + " " 41 + cur.getBalanceAmount()); 42 43 } 44 ois.close(); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 System.out.println("File Reading Success"); 49 } 50 51 @SuppressWarnings("unchecked") 52 public static void main(String[] args) { 53 Scanner reader = new Scanner(System.in); 54 CurrentAccount cu; 55 // Map map=new HashMap(); 56 Map<Integer, CurrentAccount> map = new HashMap<Integer, CurrentAccount>(); 57 for (int i = 0; i < 1; i++) { 58 cu = new CurrentAccount(); 59 60 System.out.println("Enter AccountId"); 61 cu.setAccountId(reader.nextInt()); 62 System.out.println("Enter name"); 63 cu.setName(reader.next()); 64 System.out.println("Enter balanceAmount"); 65 cu.setBalanceAmount(reader.nextDouble()); 66 map.put(10000 + i, cu); 67 } 68 69 String answer = "No"; 70 71 while (answer.equals("No") || !answer.equals("Yes")) { 72 System.out.println("Enter the path to store the data:"); 73 // File f = new File("D:\\Account.txt"); 74 String path = reader.next(); 75 File file = new File(path); 76 77 if (file.exists()) { 78 System.out 79 .println("The file already exists ,do you want it being overwritten?(Yes/No)"); 80 answer = reader.next(); 81 82 if (answer.equals("Yes")) { 83 TestCurrentAccount ta = new TestCurrentAccount(); 84 ta.objectWriter(map, file); 85 ta.objectReader(file); 86 break; 87 } else if (answer.equals("No")) { 88 System.out 89 .println("Enter the new path to store the data:"); 90 91 } else { 92 System.out 93 .println("Please enter the right answer,try again:"); 94 } 95 } else { 96 TestCurrentAccount ta = new TestCurrentAccount(); 97 ta.objectWriter(map, file); 98 ta.objectReader(file); 99 break;100 101 }102 103 }104 }105 }106 107 class CurrentAccount implements Serializable {108 int AccountId;109 String name;110 double balanceAmount;111 112 public int getAccountId() {113 return AccountId;114 }115 116 public void setAccountId(int accountId) {117 AccountId = accountId;118 }119 120 public String getName() {121 return name;122 }123 124 public void setName(String name) {125 this.name = name;126 }127 128 public double getBalanceAmount() {129 return balanceAmount;130 }131 132 public void setBalanceAmount(double balanceAmount) {133 this.balanceAmount = balanceAmount;134 }135 136 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。