public class ABCD{private String hdfsHost; { //包外配置需打开 获取项目外的文件 resources内的文件该方法无法获取到 需要下面包内方法获取到 try { Properties properties = new Properties(); FileInputStream fileInputStream = new FileInputStream(new File("./application.properties")); properties.load(fileInputStream); hdfsHost= properties.getProperty("file.hdfs.host"); } catch (IOException e) { e.printStackTrace(); } } /*{ //包内配置需打开 获取项目内的路径 class反射获取resources路径下文件 try { Properties properties = new Properties(); ClassLoader classLoader = ABCD.class.getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream("./application.properties"); properties.load(inputStream); hdfsHost= properties.getProperty("file.hdfs.host"); } catch (IOException e) { e.printStackTrace(); } }*/}
以下为application.properties 文件内容