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),"");
java读取Properties文件及赋值的更多相关文章
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- java读取properties文件时候要注意的地方
java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
- java基础—java读取properties文件
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- Java基础学习总结(15)——java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取.properties文件
在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...
随机推荐
- iOS_05_iOS设备发展史
ios设备发展史 IPhone一代 * 2007年发布. * 支持电子邮件.移动电话.短信.网络浏览等. * 采取触摸键盘 * 3.5英寸,480 x 320像素. * 后置摄像投200万像素. IP ...
- Redis 哨兵(sentinel)模式集群配置(5.0.3版本)
一.准备工作 1.系统环境:centos6.4 2.服务器六台(1主5从): 192.168.1.161(master) 192.168.1.162(slave) 192.168.1.163(slav ...
- 【习题 5-10 UVA-1597】Searching the Web
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串 ...
- Dll的链接使用细节
关于Dll Dll.Exe 都是PE格式的二进制文件. Dll相当于Linux操作系统下的so文件 1 基地址(Base Address)和相对地址(RelativeVirtual Address) ...
- HDMI ARC功能详解及应用介绍
http://www.icpcw.com/Parts/Peripheral/Skill/3260/326044_2.htm [电脑报在线]很多用户和读者购买了电视以后,都发现自己电视的HDMI接口上经 ...
- 如何在hadoop中控制map的个数 分类: A1_HADOOP 2015-03-13 20:53 86人阅读 评论(0) 收藏
hadooop提供了一个设置map个数的参数mapred.map.tasks,我们可以通过这个参数来控制map的个数.但是通过这种方式设置map的个数,并不是每次都有效的.原因是mapred.map. ...
- Classification and Representation
Classification To attempt classification, one method is to use linear regression and map all predict ...
- [CSS] Easily Reset Styles With a Single CSS value
There are times where you need to reset a an element’s styles. Instead of overwriting it with even m ...
- iOS开发Quartz2D之 七:雪花效果
#import "VCView.h" @implementation VCView -(void)awakeFromNib { //[NSTimer scheduledTimerW ...
- [内核编程] 键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试
键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试 完整源代码 /// /// @file ctrl2cap.c /// @author wowocock /// @date 2009-1 ...