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

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

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

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


  1. package com.uniquedu.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.util.Properties;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.springframework.web.context.request.RequestContextHolder;
  7. import org.springframework.web.context.request.ServletRequestAttributes;
  8. public class EmailProperRead {
  9. public String iniPath = "/WEB-INF/systemConfig.Properties"; //文件路径
  10. public Properties properties = new Properties();
  11. @SuppressWarnings("deprecation")
  12. public EmailProperRead() throws Exception{
  13. HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
  14. iniPath = request.getRealPath("/")+iniPath;
  15. File file = new File(iniPath);
  16. if(!file.exists()){
  17. file.createNewFile();
  18. }
  19. properties.load(new FileInputStream(iniPath));
  20. }
  21. }

二、取值:


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

三、赋值并保存文件:


  1. Properties properties = new EmailProperRead().properties;
  2. //发送邮件时显示的发件人名称
  3. properties.setProperty("emailName", request.getParameter("emailName"));
  4. //发件人的邮箱地址
  5. properties.setProperty("emailAddress", request.getParameter("emailAddress"));
  6. //发件人的邮箱密码
  7. properties.setProperty("emailPaswd", request.getParameter("emailPaswd"));
  8. //邮箱服务器
  9. properties.setProperty("smtpAddress", request.getParameter("smtpAddress"));
  10. //邮件主题
  11. properties.setProperty("emailTheme", request.getParameter("emailTheme"));
  12. properties.store(new FileOutputStream(new EmailProperRead(systemType).iniPath),"");

java读取Properties文件及赋值的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  3. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  4. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  5. java读取properties文件时候要注意的地方

    java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...

  6. java基础—java读取properties文件

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  7. Java基础学习总结(15)——java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  8. java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  9. java读取.properties文件

    在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...

随机推荐

  1. iOS_05_iOS设备发展史

    ios设备发展史 IPhone一代 * 2007年发布. * 支持电子邮件.移动电话.短信.网络浏览等. * 采取触摸键盘 * 3.5英寸,480 x 320像素. * 后置摄像投200万像素. IP ...

  2. 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 ...

  3. 【习题 5-10 UVA-1597】Searching the Web

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串 ...

  4. Dll的链接使用细节

    关于Dll Dll.Exe 都是PE格式的二进制文件. Dll相当于Linux操作系统下的so文件 1 基地址(Base Address)和相对地址(RelativeVirtual Address) ...

  5. HDMI ARC功能详解及应用介绍

    http://www.icpcw.com/Parts/Peripheral/Skill/3260/326044_2.htm [电脑报在线]很多用户和读者购买了电视以后,都发现自己电视的HDMI接口上经 ...

  6. 如何在hadoop中控制map的个数 分类: A1_HADOOP 2015-03-13 20:53 86人阅读 评论(0) 收藏

    hadooop提供了一个设置map个数的参数mapred.map.tasks,我们可以通过这个参数来控制map的个数.但是通过这种方式设置map的个数,并不是每次都有效的.原因是mapred.map. ...

  7. Classification and Representation

    Classification To attempt classification, one method is to use linear regression and map all predict ...

  8. [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 ...

  9. iOS开发Quartz2D之 七:雪花效果

    #import "VCView.h" @implementation VCView -(void)awakeFromNib { //[NSTimer scheduledTimerW ...

  10. [内核编程] 键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试

    键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试 完整源代码 /// /// @file ctrl2cap.c /// @author wowocock /// @date 2009-1 ...