zoukankan      html  css  js  c++  java
  • 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>() {
                @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();
                    }
                }
            }
        }
  • 相关阅读:
    How to fix the error; 'Error: Bootstrap tooltips require Tether (http://github.hubspot.com/tether/)'
    CentOS 7.2安装教程
    webpack打包生成多个vendor的配置方法
    webpack务虚扫盲
    Webpack引入jquery及其插件的几种方法
    多线程Lock版生产者和消费者模式
    多线程共享全局变量以及锁机制
    多线程Thread类
    多线程threading模块
    Python正则表达式(二)
  • 原文地址:https://www.cnblogs.com/yihaifutai/p/6754629.html
Copyright © 2011-2022 走看看