java读取Properties文件及赋值

懒得整理了,直接复制我工程里的代码:

第一个读取文件的类可以直接复制,

不复制的话,在下面取值和赋值部分的代码new EmailProperRead(systemType).properties根据自己情况修改。

一、读取文件的类EmailProperRead(一个完整文件,可以直接复制使用):

package com.uniquedu.util;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class EmailProperRead {
public String iniPath = "/WEB-INF/systemConfig.Properties"; //文件路径
public Properties properties = new Properties();
@SuppressWarnings("deprecation")
public EmailProperRead() throws Exception{
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
iniPath = request.getRealPath("/")+iniPath;
File file = new File(iniPath);
if(!file.exists()){
file.createNewFile();
}
properties.load(new FileInputStream(iniPath));
}
}

二、取值:

Properties props = new Properties(); // 获取系统环境
//获取邮箱配置信息
final Properties properties = new EmailProperRead().properties;
//发送邮件时显示的发件人名称
String personalName = properties.getProperty("emailName");
// 发件人的邮箱用户名
final String usernames = properties.getProperty("emailAddress");
// 发件人的邮箱密码
final String passwords = properties.getProperty("emailPaswd");
// 邮箱服务器
String host = properties.getProperty("smtpAddress");
//邮件主题
String mail_subject = properties.getProperty("emailTheme");

三、赋值并保存文件:

Properties properties = new EmailProperRead().properties;
//发送邮件时显示的发件人名称
properties.setProperty("emailName", request.getParameter("emailName"));
//发件人的邮箱地址
properties.setProperty("emailAddress", request.getParameter("emailAddress"));
//发件人的邮箱密码
properties.setProperty("emailPaswd", request.getParameter("emailPaswd"));
//邮箱服务器
properties.setProperty("smtpAddress", request.getParameter("smtpAddress"));
//邮件主题
properties.setProperty("emailTheme", request.getParameter("emailTheme"));
properties.store(new FileOutputStream(new EmailProperRead(systemType).iniPath),"");


转载于:https://www.cnblogs.com/jpfss/p/9790574.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值