摘要:属性文件在java项目中被用于外部化配置,例如,数据库设置。在这篇文章中,我们将看到如何在java中阅读属性文件。
Java使用Properties类存储在键值对之上。属性。load方法以键值对的形式加载属性文件非常方便。
属性文件看起来是这样的。
有两种方法可以做到这一点。
- Read properties file from system
- Read properties file from classpath
Read properties file from System :
在这一点上,您需要从系统路径读取属性文件。这里我将属性文件放在项目的根级别。
java code:
package cn.micai.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 描述:How to read properties file in java
* <p>
*
* @author: 赵新国
* @date: 2018/6/7 13:25
*/
public class ReadPropertiesFileJavaMain {
public static void main(String [] args) throws IOException {
System.out.println("Reading from properties file");
System.out.println("-----------------------------");
Properties prop = getPropertyFromSystem("src/main/resources/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("-----------------------------");
/*ReadPropertiesFileJavaMain readPropertiesFileJavaMain = new ReadPropertiesFileJavaMain();
System.out.println("Reading from properties file");
System.out.println("-----------------------------");
Properties properties = readPropertiesFileJavaMain.readPropertiesFileFromClasspath("/config.properties");
System.out.println("host : " + properties.getProperty("host"));
System.out.println("username : " + properties.getProperty("username"));
System.out.println("password : " + properties.getProperty("password"));
System.out.println("-----------------------------");*/
}
// Read properties file from System
private static Properties getPropertyFromSystem(String fileName) throws IOException {
FileInputStream fileInputStream = null;
Properties properties = null;
try {
fileInputStream = new FileInputStream(fileName);
// create Properties class object
properties = new Properties();
// load properties file into it
properties.load(fileInputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
fileInputStream.close();
}
return properties;
}
// Read properties file from classpath
public Properties readPropertiesFileFromClasspath(String fileName) throws IOException {
InputStream inputStream = null;
Properties properties = null;
try {
properties = new Properties();
inputStream = this.getClass().getResourceAsStream(fileName);
// create Properties class object
if (inputStream != null) {
// load properties file into it
properties.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return properties;
}
}
当您运行在程序之上时,您将得到以下输出:
Read properties file from classpath:
您也可以将属性文件读取到类路径。您有$project/src作为默认的classpath,因为这个src文件夹将被复制到类中。你可以把它放在$project/src文件夹中,然后从那里读取它。
你需要使用
this.getClass().getResourceAsStream("/config.properties");
去读取它从classpath下
package cn.micai.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 描述:How to read properties file in java
* <p>
*
* @author: 赵新国
* @date: 2018/6/7 13:25
*/
public class ReadPropertiesFileJavaMain {
public static void main(String [] args) throws IOException {
/*System.out.println("Reading from properties file");
System.out.println("-----------------------------");
Properties prop = getPropertyFromSystem("src/main/resources/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("-----------------------------");*/
ReadPropertiesFileJavaMain readPropertiesFileJavaMain = new ReadPropertiesFileJavaMain();
System.out.println("Reading from properties file");
System.out.println("-----------------------------");
Properties properties = readPropertiesFileJavaMain.readPropertiesFileFromClasspath("/config.properties");
System.out.println("host : " + properties.getProperty("host"));
System.out.println("username : " + properties.getProperty("username"));
System.out.println("password : " + properties.getProperty("password"));
System.out.println("-----------------------------");
}
// Read properties file from System
private static Properties getPropertyFromSystem(String fileName) throws IOException {
FileInputStream fileInputStream = null;
Properties properties = null;
try {
fileInputStream = new FileInputStream(fileName);
// create Properties class object
properties = new Properties();
// load properties file into it
properties.load(fileInputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
fileInputStream.close();
}
return properties;
}
// Read properties file from classpath
public Properties readPropertiesFileFromClasspath(String fileName) throws IOException {
InputStream inputStream = null;
Properties properties = null;
try {
properties = new Properties();
inputStream = this.getClass().getResourceAsStream(fileName);
// create Properties class object
if (inputStream != null) {
// load properties file into it
properties.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return properties;
}
}
当您运行在程序之上时,您将得到以下输出: