学习Android开发step001-反转一个三位整数-IO流(Properties概述、存取、存取配置文件)

一:学习Android开发:

老师用的Android studio版本是2020.3.1。视频P1-P4讲的android studio的安装,我本机之前已经装了。下一步学习P5。

二:problem37:反转一个三位整数。

class Solution {
public:
    int reverseInteger(int number) {
        // 原个位
        int onesPlace = number % 10;
        // 原十位
        int tensPlace = (number / 10) % 10;
        // 原百位
        int hundredsPlace = number / 100;
        
        return onesPlace * 100 + tensPlace * 10 + hundredsPlace;
    }
};

 三:IO流(Properties概述、存取、存取配置文件):P260、P261、P262:

/*
Properties是hashtable的子类。
也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。

是集合中和IO技术相结合的集合容器。

该对象的特点:可以用于键值对形式的配置文件。

那么在加载数据时,需要数据有固定格式:键=值。
*/
import java.io.*;
import java.util.*;
class PropertiesDemo {
    public static void main(String[] args) throws IOException {
//        setAndGet();
//        method_1();
        loadDemo();
    }

    public static void loadDemo() throws IOException {
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("info.txt");

        // 将流中的数据加载进集合。
        prop.load(fis);

        prop.setProperty("wangwu", "39");

        FileOutputStream fos = new FileOutputStream("info.txt");

        prop.store(fos, "haha");

//        sop(prop);
        prop.list(System.out);

        fos.close();
        fis.close();
    }

    // 演示,如何将流中的数据存储到集合中。
    // 想要将info.txt中键值数据存到集合中进行操作。
    /*
        1.用一个流和info.txt文件关联。
        2.读取一行数据,将该行数据用“=”进行切割。
        3.等号左边作为键,右边作为值。存入到Properties集合中即可。
    */
    public static void method_1() throws IOException {
        BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));

        String line = null;
        Properties prop = new Properties();
        while ((line = bufr.readLine()) != null) {
            String[] arr = line.split("=");
//            sop(line);
//            sop(arr[0] + "....." + arr[1]);
            prop.setProperty(arr[0], arr[1]);
        }

        bufr.close();

        sop(prop);
    }

    // 设置和获取元素
    public static void setAndGet() {
        Properties prop = new Properties();

        prop.setProperty("zhangsan", "30");
        prop.setProperty("lisi", "39");

//        sop(prop);
        String value = prop.getProperty("lisi");
//        sop(value);

        prop.setProperty("lisi", "89");

        Set<String> names = prop.stringPropertyNames();
        for(String s : names) {
            sop(s + ":" + prop.getProperty(s));
        }
    }

    public static void sop(Object obj) {
        System.out.println(obj);
    }
}

 下一步学习P263。

posted on 2022-01-09 21:25  平凡力量  阅读(31)  评论(0编辑  收藏  举报