javase(Properties集合及学生对象信息录入文本中案例)
生活随笔
收集整理的這篇文章主要介紹了
javase(Properties集合及学生对象信息录入文本中案例)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、Properties集合
Properties和IO流的結合使用
1 public void load(Reader reader) 2 public void store(Writer writer,String comments) 3 /* 4 * 這里的集合必須是Properties集合: 5 * public void load(Reader reader):把文件中的數據讀取到集合中 6 * public void store(Writer writer,String comments):把集合中的數據存儲到文件 7 * 8 */ 9 public class PropertiesDemo3 { 10 public static void main(String[] args) throws IOException { 11 // myLoad(); 12 13 myStore(); 14 } 15 16 private static void myStore() throws IOException { 17 // 創建集合對象 18 Properties prop = new Properties(); 19 20 prop.setProperty("林青霞", "27"); 21 prop.setProperty("張三", "30"); 22 prop.setProperty("李四", "18"); 23 24 //public void store(Writer writer,String comments):把集合中的數據存儲到文件 25 Writer w = new FileWriter("name.txt"); 26 prop.store(w, "helloworld"); 27 w.close(); 28 } 29 30 private static void myLoad() throws IOException { 31 Properties prop = new Properties(); 32 33 // public void load(Reader reader):把文件中的數據讀取到集合中 34 // 注意:這個文件的數據必須是鍵值對形式 35 Reader r = new FileReader("prop.txt"); 36 prop.load(r); 37 r.close(); 38 39 System.out.println("prop:" + prop); 40 } 41 }2、鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績),按照總分從高到低存入文本文件
?
package com.lianxi1;import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Comparator; import java.util.Scanner; import java.util.TreeMap; import java.util.TreeSet;public class StudnentDemo {public static void main(String[] args) {TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num1=s2.getSun()-s1.getSun();int num2=num1==0?s2.getChinese()-s1.getChinese():num1;int num3=num2==0?s2.getMath()-s1.getMath():num2;int num4=num3==0?s2.getEnglish()-s1.getEnglish():num3;int num5=num4==0?s2.getName().compareTo(s1.getName()):num4;return num5;}});Scanner s=new Scanner(System.in);for(int i=1;i<=5;i++){System.out.println("請輸入第"+i+"個學生的姓名:");String name=s.nextLine();System.out.println("請輸入第"+i+"個學生的語文成績:");String chinese=s.nextLine(); System.out.println("請輸入第"+i+"個學生的數學成績:");String math=s.nextLine();System.out.println("請輸入第"+i+"個學生的英語成績");String english=s.nextLine();Student stu=new Student(name,Integer.parseInt(chinese),Integer.parseInt(math),Integer.parseInt(english));treeSet.add(stu);}BufferedWriter bw=null;try {bw = new BufferedWriter(new FileWriter("stuInfo.txt"));bw.write("學生成績如下:");bw.newLine();bw.write("姓名,語文,數學,英語 ");bw.newLine();bw.flush();for (Student stu : treeSet) {StringBuilder sb=new StringBuilder();sb.append(stu.getName()).append(",").append(stu.getChinese()).append(",").append(stu.getMath()).append(",").append(stu.getEnglish());bw.write(sb.toString());bw.newLine();bw.flush();}System.out.println("學生信息錄入完畢");} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}finally{if(bw!=null){try {bw.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}}}?
轉載于:https://www.cnblogs.com/yihaifutai/p/6754629.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的javase(Properties集合及学生对象信息录入文本中案例)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从数组里挑出仅仅出现一次的对象
- 下一篇: VMware如何进入安全模式