• volicity 模板类,java操作配置文件


    import java.io.StringWriter;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    
    import org.apache.velocity.Template;
    import org.apache.velocity.VelocityContext;
    import org.apache.velocity.app.Velocity;
    import org.apache.velocity.context.Context;
    
    
    public class VMRenderUtils {
    
        static public final String vmfileName = "email.vm";
        static private final Properties  p      = new Properties();
        static {
            p.put("file.resource.loader.class",
                  "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            Velocity.init(p);
        }
    
        static public String render(String flag, String emailSubject, String emailHost, 
                String emailFrom, String emailPassword, Map<String, HashSet<String>> moduleInCharge) {
            Template template = Velocity.getTemplate(vmfileName);
            
            StringBuilder moduleInChargeString = new StringBuilder();
            
            Set<String> key = moduleInCharge.keySet();
            for (Iterator<String> it = key.iterator(); it.hasNext();) {
                String s = (String) it.next();
                moduleInChargeString.append(s+"="+HashSetToString(moduleInCharge.get(s))+"
    ");
            }
           
            Context context = buildContext(flag, emailSubject, emailHost, 
                     emailFrom,  emailPassword,  moduleInChargeString.toString());
            StringWriter sw = new StringWriter();
            template.merge(context, sw);
            return sw.toString();
        }
    
        static Context buildContext(String flag, String emailSubject, String emailHost, 
                String emailFrom, String emailPassword, String moduleInCharge) {
            Context context = new VelocityContext();
            context.put("flag", flag);
            context.put("emailSubject", emailSubject);
            context.put("emailHost", emailHost);
            context.put("emailFrom", emailFrom);
            context.put("emailPassword", emailPassword);
            
            context.put("moduleInCharge", moduleInCharge);
    
            return context;
        }
        
        private static String HashSetToString(HashSet<String> hashSet) {
            String result = "";
            if(hashSet == null) {
                return result;
            }
            
            for(String at:hashSet) {
                result += (at+",");
            }
            if(!isBlank(result)) {
                result = result.substring(0,result.length()-1);
            }
            return result;
        }
        private static boolean isBlank(String value) {
            if(value == null || "".equals(value)) {
                return true;
            }
            return false;
        }
    }

    email.vm文件

    SUPER_ADMIN=huahuiyang@gmail.com
    
    #whether to send email
    LOL_EMAIL=${flag}
    
    #email subject
    EMAIL_SUBJECT=${emailSubject}
    EMAIL_HOST=${emailHost}
    EMAIL_FROM=${emailFrom}
    EMAIL_PASSWORD=${emailPassword}
    
    #send mail to....
    ${moduleInCharge}

    java如何调用:

    URL filePath = Thread.currentThread().getContextClassLoader().getResource("email.properties");
            File file = new File(filePath.toString().substring(5));
            FileWriter fw;
            try {
                fw = new FileWriter(file);
                fw.write(configureFile);
                fw.close();
            } catch (IOException e) {
                logger.warn("fail to update configure file email.properties");
            } 
  • 相关阅读:
    蒲公英
    大神-YY
    iOS开发精选知识点讲解 - 视频等 iOSStrongDemo是由@李刚维护,总结一些iOS开发精选知识点。每一个知识点都有相应的测试代码,非常适合iOS初学者。
    iOS开发UI篇—懒加载
    iOS开发UI篇—UITableviewcell的性能优化和缓存机制
    iOS开发UI篇—UITableview控件基本使用
    iOS开发UI篇—UITableview控件简单介绍
    iOS — Autolayout之Masonry解读
    iOS开发UI篇—多控制器和导航控制器简单介绍
    iOS开发网络篇—数据缓存
  • 原文地址:https://www.cnblogs.com/yanghuahui/p/3223822.html
Copyright © 2020-2023  润新知