JAVA开发10--加载相对路径下配置文件并读取内容
2016-10-20 17:15
661 查看
描述:
java开发,使用 FileInputStream 加载 文件,读取文件内容。
在项目文件夹的同目录下,有一个文件夹 config,此文件夹下有一个配置文件 var2.properties;
文件内容如下:
YEAR:2016
MONTH:07
DATA_TYPE:OA_ProjectBudget
代码如下:
package com.noahark.dao;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Test {
public static void main(String[] args) throws IOException {
//加载配置文件
FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\config\\var2.properties");
Properties properties = new Properties();
if(fis !=null)
properties.load(fis);
// 获取信息
String year_value = properties.getProperty("YEAR");
String month_value = properties.getProperty("MONTH");
String data_type = properties.getProperty("DATA_TYPE");
int year = Integer.parseInt(year_value);
int month = Integer.parseInt(month_value);
System.out.println("year: " + year + "\nmonth: " + month + "\ndata_type: " + data_type);
}
}
输出结果如下:
year: 2016
month: 7
data_type: OA_ProjectBudget
java开发,使用 FileInputStream 加载 文件,读取文件内容。
在项目文件夹的同目录下,有一个文件夹 config,此文件夹下有一个配置文件 var2.properties;
文件内容如下:
YEAR:2016
MONTH:07
DATA_TYPE:OA_ProjectBudget
代码如下:
package com.noahark.dao;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Test {
public static void main(String[] args) throws IOException {
//加载配置文件
FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\config\\var2.properties");
Properties properties = new Properties();
if(fis !=null)
properties.load(fis);
// 获取信息
String year_value = properties.getProperty("YEAR");
String month_value = properties.getProperty("MONTH");
String data_type = properties.getProperty("DATA_TYPE");
int year = Integer.parseInt(year_value);
int month = Integer.parseInt(month_value);
System.out.println("year: " + year + "\nmonth: " + month + "\ndata_type: " + data_type);
}
}
输出结果如下:
year: 2016
month: 7
data_type: OA_ProjectBudget
相关文章推荐
- 资源加载 - 相对路径获取JAVA配置文件
- JAVA使用相对路径读取配置文件
- Java相对路径读取文件
- java 读取properties配置文件内容乱码 --日文乱码对应方法
- java获取当前路径及加载配置文件
- java配置文件读取和项目路径的多种获取方式
- Java相对路径读取文件
- JAVA相关问题:java读取properties配置文件路径问题
- Java相对路径读取文件
- JAVA 使用相对路径读取文件
- Java中框架的概念,以及从配置文件中读取配置信息并加载指定类
- Java读取相对路径文件
- JAVA读取配置文件路径
- Java相对路径读取文件
- 读取本机的Java运行环境和相关配置文件的内容 推荐
- Java相对路径读取文件
- java 使用相对路径读取文件【转】
- java读取相对路径文件
- java 使用相对路径读取文件
- java 使用相对路径读取文件