Java中读存properties文件的配置信息

配置文件:config.properties

配置文件的路径:src/main/resources/static/config.properties

配置文件config.properties内容如下:(用来测试的需要读取的数据,key-value):

一、读取properties文件中的某个值

#config.properties配置
ip = 127.0.0.1

方法一: 

//引入包
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * 读取config.properties文件中ip值
 */
public void readProperties() {
	Properties pro = new Properties();
	// 使用InPutStream流读取properties文件
	BufferedReader bufferedReader;
	try {
		//读取本地目录下的properties文件
		bufferedReader = new BufferedReader(new FileReader("D:/config.properties"));
		//读取项目中resources下的properties文件
		//bufferedReader = new BufferedReader(new FileReader("src/main/resources/config.properties"));
		pro.load(bufferedReader);
	} catch (IOException e) {
		e.printStackTrace();
	}
    String ip = String.valueOf(pro.get("ip"));
}

方法二:

/**
 * 读取config.properties文件中ip值
 */
public Integer readProperties() {
	Properties pro = new Properties();
	try {
		// 读取项目中resource下的properties文件
		InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties"); 
		BufferedReader reader;
		reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
		pro.load(reader);
	} catch (Exception e) {
		e.printStackTrace();
	}
	String str = String.valueOf(pro.get("ip"));
}

二、修改properties文件中的某个值

#config.properties配置
ip = 127.0.0.1

方法一 

//引入包
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 修改config.properties文件中ip值
 */
public void updateProperties() {
	Properties pro = new Properties();
	pro.setProperty("ip", "192.168.1.1");
	FileOutputStream output = null;
	try {
        //项目中resources下的properties文件
		output = new FileOutputStream("src/main/resources/config.properties");
		//将Properties中的属性列表(键和值)写入输出流
		pro.store(output, "");
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

方法二

/**
 * 修改config.properties文件中ip值
 */
public void updateProperties() {
	String filePath="F:/config.properties";
	File file=new File(filePath);
	Properties pro = new Properties();
	try {
		pro.load(new FileInputStream(file));
		pro.setProperty("ip","192.168.1.1");
		FileOutputStream output = new FileOutputStream(filePath);
		pro.store(output, "The New properties file");
		output.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

===============================================================================

三、使用阿里的fastjson解析

#config.properties配置
ip = 127.0.0.1
port = 8080
filepath = D:/download
username = root
password = 123456
<!-- fastjson包 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.83</version>
</dependency>
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import com.alibaba.fastjson.JSONObject;

public class getPropertys {
	public static void main(String[] args) throws IOException {
		Properties pro = new Properties();
		// 使用InPutStream流读取properties文件
		BufferedReader bufferedReader = new BufferedReader(new FileReader("src/main/resources/static/config.properties"));
		pro.load(bufferedReader);
		JSONObject obj = new JSONObject();
                //取出properties中的数据,并赋值
		obj.put("ip",pro.getProperty("ip"));
		obj.put("port",pro.getProperty("port"));
		obj.put("filepath",pro.getProperty("filepath"));
		obj.put("username",pro.getProperty("username"));
		obj.put("password",pro.getProperty("password"));
		System.out.println("输出Object对象:" + obj.toString());
	}
}

四、使用google的gson解析

#config.properties配置
ip = 127.0.0.1
port = 8080
filepath = D:/download
username = root
password = 123456
<!-- gson包 -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import com.google.gson.JsonObject;

public class getPropertys {
	public static void main(String[] args) throws IOException {
		Properties properties = new Properties();
		// 使用InPutStream流读取properties文件
		BufferedReader bufferedReader = new BufferedReader(new FileReader("src/main/resources/static/config.properties"));
		properties.load(bufferedReader);
		JSONObject obj = new JSONObject();
		JsonObject obj=new JsonObject();
		obj.addProperty("ip",properties.getProperty("ip"));
		obj.addProperty("port",properties.getProperty("port"));
		obj.addProperty("filepath",properties.getProperty("filepath"));
		obj.addProperty("username",properties.getProperty("username"));
		obj.addProperty("password",properties.getProperty("password"));
		System.out.println("输出Object对象:" + obj.toString());
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学弟不想努力了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值