Idea配置文件的读取

开发过程中遇到配置文件读取问题,因此记录以后运用的到。

配置文件位置:

配置文件内容:

default_size = 100
grid_size = 20
delayTime = 200

配置文件读取方式:

public void initProperties() {
        Properties properties = new Properties();
        String path = LifeGame.class.getClassLoader().getResource("lifeGame.properties").getPath();
        File file = new File(path);
        try {
            FileInputStream inputStream = new FileInputStream(file);
            properties.load(inputStream);
            delay = Integer.parseInt(properties.getProperty("delayTime"));
            tableSize = Integer.parseInt(properties.getProperty("default_size"));
            gridSize = Integer.parseInt(properties.getProperty("grid_size"));
            currentMap = new int[tableSize][tableSize];
            neighbors = new int[tableSize][tableSize];
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
原文地址:https://www.cnblogs.com/huangyichun/p/6947092.html