如何在Java中读取属性文件
时间:2020-02-23 14:34:22 来源:igfitidea点击:
在本教程中,我们将看到如何在Java中读取属性文件。 Properties文件用于Java项目到Enterialise配置,例如数据库设置。
Java使用 Properties程序存放上述 key-values一对。 Properties.load()方法非常方便地以形式加载属性文件 key-values对。
你可以做两种方式。
从系统中读取属性文件
在此,我们需要阅读 properties从系统路径中的文件。
其中我将属性文件放在一个项目的根级别。
Java代码:
package org.igi.theitroad;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/*
* @author igi Mandliya
*/
public class ReadPropertiesFileJavaMain {
public static void main(String args[]) throws IOException {
System.out.println("Reading from properties file");
System.out.println("-----------------------------");
Properties prop = readPropertiesFile("config.properties");
System.out.println("host : " + prop.getProperty("host"));
System.out.println("username : " + prop.getProperty("username"));
System.out.println("password : " + prop.getProperty("password"));
System.out.println("-----------------------------");
}
public static Properties readPropertiesFile(String fileName) throws IOException {
FileInputStream fis = null;
Properties prop = null;
try {
fis = new FileInputStream(fileName);
//create Properties class object
prop = new Properties();
//load properties file into it
prop.load(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
fis.close();
}
return prop;
}
}
运行上面的程序时,我们将获取以下输出:
Reading from properties file ————————— host : localhost username : theitroad password : java123 —————————
读取类路径的属性文件
我们可以读取属性文件 classpath也。
你有 $project/src默认为 classpath由于此SRC文件夹将被复制到类。
你可以把它放进去 $project/src文件夹并从那里读取它。
你需要使用 this.getClass().getResourceAsStream("/config.properties");" 阅读它classpath` 。
package org.igi.theitroad;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/*
* @author igi Mandliya
*/
public class ReadPropertiesFileJavaMain {
public static void main(String args[]) throws IOException {
ReadPropertiesFileJavaMain rp = new ReadPropertiesFileJavaMain();
System.out.println("Reading from properties file");
System.out.println("-----------------------------");
Properties prop = rp.readPropertiesFile("/config.properties");
System.out.println("host : " + prop.getProperty("host"));
System.out.println("username : " + prop.getProperty("username"));
System.out.println("password : " + prop.getProperty("password"));
System.out.println("-----------------------------");
}
public Properties readPropertiesFile(String fileName) throws IOException {
InputStream fis = null;
Properties prop = null;
try {
prop = new Properties();
fis = this.getClass().getResourceAsStream(fileName);
//create Properties class object
if (fis != null) {
//load properties file into it
prop.load(fis);
} else {
throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
fis.close();
}
return prop;
}
}

