• 使用System.getProperty("user.dir")获取项目下的文件内容


    System.getProperty("user.dir")的作用是获取到项目所在的绝对路径,使用这个api就能获取项目下的文件

    例如我想获取项目下/src/main/resources/config/certificate.properties的内容,可以使用如下代码:

        public static Properties getProperties(String pathInDemo) throws IOException {
            Properties properties = new Properties();
    
            String path = System.getProperty("user.dir") + "/src/main/resources/" + pathInDemo;
            
            File file = new File(path);
    
            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new FileReader(file));
                properties.load(bufferedReader);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return properties;
        }
    
        public static void main(String[] args) throws IOException {
            System.out.println("libreoffice.path=" + getConfig("config\certificate.properties", "libreoffice.path"));
            System.out.println(
                    "certificate.image.suffix=" + getConfig("config\certificate.properties", "certificate.image.suffix"));
        }

    以这样拼接路径的方式得到项目下指定文件的绝对路径,需要注意的是,System.getProperty("user.dir")获取到的项目路径以项目名称结尾,不带"/"。

  • 相关阅读:
    区块链,去中心化应用基本知识与开发实践学习
    服务铝料门窗基本资料
    微信小游戏发布注意事项
    2018阿里云短信发送DEMO接入简单实例
    #SQL1242错误
    百度站内搜索
    jqGrid 手册
    4步 —— 快速开始运行直播小程序
    数字平滑 前端插件JS&CSS库
    jqGrid 中文配置
  • 原文地址:https://www.cnblogs.com/xhj123/p/12571710.html
Copyright © 2020-2023  润新知