码迷,mamicode.com
首页 > 编程语言 > 详细

javase(Properties集合及学生对象信息录入文本中案例)

时间:2017-04-24 00:06:52      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:txt   rri   scan   div   finally   ade   int   ext   chinese   

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>() {
            @Override
            public 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();
                }
            }
        }
    }

 

javase(Properties集合及学生对象信息录入文本中案例)

标签:txt   rri   scan   div   finally   ade   int   ext   chinese   

原文地址:http://www.cnblogs.com/yihaifutai/p/6754629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!