第三周遗忘总结

1、String类型转化成int类型

String str = "890";
int num = Integer.parseInt(str);

2、Calcendar

public class Test7 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入年份:");
        int year = scanner.nextInt();
        System.out.print("请输入月份:");
        int month= scanner.nextInt();
        System.out.print("请输入日期:");
        int day  = scanner.nextInt();

        String[] weekDays = DateFormatSymbols.getInstance(Locale.CHINA).getWeekdays();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DATE, day);
        int week = cal.get(Calendar.DAY_OF_WEEK);
        System.out.println(sdf.format(cal.getTime())+"是");
        System.out.println(weekDays[week]);

    }

}

3、Properties

    public void changeSecret(String source,String name,String secret){

        Properties pro = new Properties();
        Scanner sc = new Scanner(System.in);
        try {

            pro.load(new FileReader(source));
            if(pro.containsKey(name)){
                if(pro.getProperty(name).equals(secret)){
                    System.out.println("请输入新密码:");
                    String sec = sc.next();
                    pro.setProperty(name, sec);
                    pro.store(new FileWriter(source), "");
                    System.out.println("修改成功");
                }else{
                    System.out.println("密码错误");
                }
            }else{
                System.out.println("用户名不存在");
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

利用相对路径读取文件:

public  void readsrc(){
        Properties properties=new Properties();
        //通过程序获得src下面的文件的输入流
        try {
            properties.load(this.getClass().getClassLoader().getResourceAsStream("users.properties"));

            System.out.println(properties.getProperty("name"));
            System.out.println(properties.getProperty("pass"));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

4、可序列化Serializable
(1)现实现Serializable接口

public class Students implements Serializable{

    private String name;
    private String number;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}

(2)直接将对象写入或读出

public void writeSerializable(String source){

        ArrayList<Students> list = new ArrayList<Students>();

        try {

            BufferedReader reader = new BufferedReader(new FileReader(source));
            String str = reader.readLine();
            while(str != null){
                Students stu = new Students();
                String[] s = str.split(":");
                stu.setNumber(s[0]);
                stu.setName(s[1]);
                list.add(stu);
                str = reader.readLine();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {

            ObjectOutputStream objectOutput = new ObjectOutputStream(new FileOutputStream("e://students.txt"));
            objectOutput.writeObject(list);
            objectOutput.flush();
            objectOutput.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值