【发布时间】:2018-06-07 05:52:06
【问题描述】:
我在 app 文件夹中有一个配置文件 config.properties。我在build.gradle 中用于构建配置。
我需要用 java 代码读取这个文件。但我可以弄清楚我应该使用path。我如何在 java 代码中读取这个文件。下面的代码给了我FileNotFoundException
try {
Properties properties = new Properties();
File inputStream=new File("/config.properties");
properties.load(new FileInputStream(inputStream));
return properties.getProperty("BASE_URL");
}catch (IOException e){
e.printStackTrace();
}
我在build.gradle 中使用相同的文件并且它运行良好。如下。
defaultConfig {
Properties versionProps = new Properties()
versionProps.load(new FileInputStream(file('config.properties')))
def properties_versionCode = versionProps['VERSION_CODE'].toInteger()
def properties_versionName = versionProps['VERSION_NAME']
def properties_appid= versionProps['APPLICATION_ID']
applicationId properties_appid
minSdkVersion 14
targetSdkVersion 26
versionCode properties_versionCode
versionName properties_versionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
build.gradle 部分工作正常,我可以分配属性。但我需要在 java 类中读取相同的文件。 File 应该使用什么路径。
配置文件如下:
VERSION_NAME=1.0.0
VERSION_CODE=1
APPLICATION_ID=com.app.drecula
BASE_URL=https://reqres.in/
【问题讨论】:
-
您是否尝试在您的应用程序中读取它?
-
是的,该文件位于与
google_play_service.json平行的app文件夹下 -
我能问一下你想从配置文件中得到什么吗? ?
标签: android gradle android-file