java读取Properties文件及赋值
2024-08-31 20:38:51
懒得整理了,直接复制我工程里的代码:
第一个读取文件的类可以直接复制,
不复制的话,在下面取值和赋值部分的代码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),"");
最新文章
- Redis(二) 扩展
- 【自己给自己题目做】:如何在Canvas上实现魔方效果
- Vue.js组件示例
- 开发者如何利用工具快速开发出完美APP
- error MIDL2311 : statements outside library block are illegal in mktyplib compatability mode
- 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符
- GDI+
- 用生活例子来解释Java synchronized块
- 使用webview如何做超时判断
- 海量数据存储之Key-Value存储简介
- JAVA面试精选
- 笨办法用js屏蔽被http劫持的浮动广告
- shell脚本—— 字符串操作(长度,查找,替换)
- Session提要
- Python转页爬取某铝业网站上的数据
- centos 检测aufs 并安装
- mongodb 设置权限
- Linux中使用Electronic WeChat客户端
- 假期训练五(poj-1077bfs+康拓展开,hdu-2577dp)
- PHP关于按位取反结果的推导过程
热门文章
- 【例题 6-15 UVA - 10129】Play on Words
- IDEACould not autowire. No beans of 'xxxMapper' type found.
- [D3] Basic Interactivity with D3 v4
- LA 5902 - Movie collection 树状数组(Fenwick树)
- 9.7 Binder系统_c++实现_编写程序
- ivotal-tc-Server与Tomcat区别
- spark提交应用的方法(spark-submit)
- js进阶 12-8 如何知道上一个函数的返回值是什么(如何判断上一个函数是否执行成功)
- 【rlz01】完全数
- [CSS] Use Generated Content to Augment Information