任何类型都可以,内容也是,excel虽然也可以生成,但它会把所有内容都放到第一个,所以还是老实用一般方法吧。

这个有点麻烦,我们一步步来(可以先拉到最后看一眼配置文件会比较好理解)

第一个类:调用方法

public void test(){//参数一是需要放进去的内容,具体肯定不会这么短(根据模板多个的情况下改用list)//参数二是读取配置文件的路径insertContent("111", "D:/dev/workspace/lzpt/resource/");
}

第二个类:基础方法类

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;import com.flatform.cfg.domain.BasicCfgMessage;public class CodeGenerator {public static String flag = "0";private String classPackage;private String codePath;private String sourcePath;private BasicCfgMessage message;//各种路径public void config(String classPackage, String codePath) {this.classPackage = classPackage;this.codePath = codePath;//这里影响生成的文件被放在哪里this.sourcePath = (codePath + "/content/");}//上面调用的就是这个方法public static void insertContent(String content, String path) throws SQLException {CodeGenerator dg = new CodeGenerator();//你的路径配置文件的路径(如果放在resource(根目录)下面的话,直接写名字)String propertFilePath = "template.config.properties";String classPackage = StringUtil.getPropertyFromFile(path, propertFilePath, "classPackage");String codePath = StringUtil.getPropertyFromFile(path, propertFilePath, "codePath");dg.config(classPackage, codePath);return dg.generator(content, path);}public void generator(String content, String path) {Map<String, Object> map = new HashMap<String, Object>();// 实例化一个map//根据配置文件的参数可能有多个,这里只有一个content,因为只配了contentmap.put("content", content);//取名字,随便取吧String name = UUIDUtils.create();try {//第一个参数是内容配置文件,可以配置多个,多个的话,这段代码复制粘贴几遍就行了//第四个参数(就是那个有.exe的,是文件类型,什么后缀出来的就是什么类型的文件。exe当然也可以生成,虽然根据内容可能无法运行(还是txt,doc这种观赏类的比较适用)……)VelocityInfoOp.generatorCode("template.vm", map, this.sourcePath, name + ".exe", path);//没什么意义,看一眼信息用的System.out.println("***************代码生成完成******************");System.out.println("代码路径:" + this.codePath);System.out.println("包:" + this.classPackage);System.out.println("********************************************");} catch (Exception e) {throw new RuntimeException();}}public String getClassPackage() {return this.classPackage;}public void setClassPackage(String classPackage) {this.classPackage = classPackage;}public String getPath() {return this.sourcePath;}public void setPath(String path) {this.sourcePath = path;}
}

下一个类:配路径用的

public static synchronized String getPropertyFromFile(String path, String filename, String key) {// getProperty获取指定键指示的系统属性//获取路径配置文件时的路径,具体情况具体分析String paodingAnalysisPath = path + filename;// 定义一个输入流InputStream in1 = null;// 定义一个类,资源包包含特定于语言环境的对象(需要特定语言环境的的资源时,加载资源包内的信息)ResourceBundle rb = null;try {in1 = new BufferedInputStream(new FileInputStream(paodingAnalysisPath));rb = new PropertyResourceBundle(in1);} catch (Exception e) {e.printStackTrace();}return rb.getString(key).trim();// 去掉空格,返回内容
}

最后一个类:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;public class VelocityInfoOp {public static void generatorCode(String templateFile, Map<String,Object> contextMap, String path, String fileName,String vmpath){VelocityContext context = new VelocityContext();VelocityEngine ve = new VelocityEngine();
   //获取内容配置文件的路径,同样具体情况具体分析String vPath = vmpath +"template";System.out.println(vPath);ve.setProperty("file.resource.loader.path", vPath);ve.setProperty("input.encoding", "UTF-8");ve.setProperty("output.encoding", "UTF-8");ve.init();for (Iterator<String> i$ = contextMap.keySet().iterator(); i$.hasNext();) {   String key = i$.next();context.put(key, contextMap.get(key));}Template template = null;try{template = ve.getTemplate(templateFile);}catch (Exception e){e.printStackTrace();}StringWriter sw = new StringWriter();if (template != null) {template.merge(context, sw);}File pathTemp = new File(path);if (!pathTemp.exists()){pathTemp.mkdirs();}writeFile(pathTemp + "/" + fileName, sw.toString());}public static void writeFile(String filePathAndName, String fileContent){try{File f = new File(filePathAndName);if (!f.exists()) {f.createNewFile();}OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");BufferedWriter writer = new BufferedWriter(write);writer.write(fileContent);writer.close();} catch (Exception e) {System.out.println("写文件内容操作出错");e.printStackTrace();}}
}

最后放一下配置文件里面的内容

template.config.properties里面:

author=SOMEONE
classPackage=com.test
codePath=/dev/workspace/test/src

就是些路径,方便改,你也可以直接写在类里面

template.vm里面:

${content}

这里说明一下

如果配的是:

内容为:${content}

前面map里面,content的内容为111

文件内容最终会是“内容为:111”

参数可以是多个,几个参数map就放几个值,键名与参数名一致就行

以上。

转载于:https://www.cnblogs.com/IceBlueBrother/p/8423105.html

生成任意内容任意类型的文件相关推荐

  1. 在SQL Server中保存和输出任意类型的文件

    我们可以把任意类型的文件保存到SQL Server中,在进行例子之前,先建立测试用表格,TestFile.sql: if exists (select * from dbo.sysobjects wh ...

  2. kesioncms (科讯cms) 6.x-8.x版本写入任意内容文件漏洞

    发布时间:2013-04-08  关键词:kesioncms 漏洞 漏洞版本:kesioncms(科讯cms) 6.x - 8.x 漏洞简介:未对提交参数判断,导致可将任意内容的文件写入网站服务器 漏 ...

  3. Windows API 实现查找、删除任意类型的文件_VERSION20120605(vc6.0调试通过)(2012.6.5最新修改)

    1 // FileToolsV20120605.cpp : Defines the entry point for the application. 2 // 3 //用vc6.0建立一个win32简 ...

  4. 文件加解密(任意文件类型的文件)

    直接上代码: 支持任意类型的文件的加密和解密 可以自由修改密钥 需要修改文件路径 文件加解密. #define _CRT_SECURE_NO_WARNINGS #include <stdlib. ...

  5. ThinkCMF框架任意内容包含漏洞

    ThinkCMF简介 ThinkCMF是一款基于PHP+MYSQL开发的中文内容管理框架,底层采用ThinkPHP3.2.3构建.ThinkCMF提出灵活的应用机制,框架自身提供基础的管理功能,而开发 ...

  6. tf 风格迁移 任意内容 固定风格 vgg19

    原文链接: tf 风格迁移 任意内容 固定风格 vgg19 上一篇: tf 风格迁移 固定内容 固定风格 vgg19 下一篇: tf scipy misc 模块 tf image 模块 图片 padd ...

  7. 蛋壳公寓信息/信息网爬取/小猪短租/豆瓣/拉钩/人民邮电报/百度电视剧/加载更多-获取字符串中间任意内容

    调试 import requests from lxml import etreebase_url = 'https://www.danke.com/room/bj?page=1'headers = ...

  8. 动态生成MSI包的WIX脚本文件内容

    生成MSI脚本批命令 例如1: set wixpath=G:\U8V13.1安装程序包\Wix set packpath="G:\U8V13.1安装程序包\所有补丁文件包" set ...

  9. python使用fpdf生成pdf章节(chapter)文件包含:页眉、页脚、章节主体、章节内容等;

    python使用fpdf生成pdf章节(chapter)文件包含:页眉.页脚.章节主体.章节内容等: 目录

最新文章

  1. OD调试9—实例:深入分析代码完成软件破解
  2. AAAI Fellow 2019名单公布!罗杰波、刘欢等人入选
  3. 如何对比_潭酒红潭酱酒对比红花郎怎么样口感如何
  4. 一般java,【面试题】java一般
  5. 软件实习周软件设计报告
  6. 台式计算机怎么关闭无线网络,台式机无线网卡被禁用了如何解决
  7. 电阻单位符号Ω怎么读?
  8. 组装服务器实训总结,计算机组装与维护实训总结
  9. 微信特殊字符包括颜文字、表情的后台存储及前端展示方法
  10. 面试常见问题之OWASP top10
  11. 计算机网络第七版(谢希仁)第二章——物理层课后习题答案
  12. Resharp 破解
  13. 留言管理系统的设计与实现
  14. pyspider 爬取bing壁纸
  15. java语言程序设计在线作业_中石油北京2018秋 《Java语言程序设计》第二次在线作业...
  16. java访问局域网共享资源
  17. 【sas】tabulate报表
  18. Linux keytool命令密钥和证书管理工具,生成ssl证书
  19. ajax实现下载文件进度条及方法详解
  20. 免费清新风教育教学通用PPT模板

热门文章

  1. Sublime Text 3 快捷键总结
  2. vue商城项目开发:封装banner组件、组件参数传递
  3. arthas命令使用示例:monitor监视指定方法的执行情况
  4. arthas命令整理:基础命令、jvm相关、class相关命令
  5. docker容器间双向通信(基于Bridge网桥)
  6. redis set数据类型常用命令及应用场景
  7. 智能判断图片中是否存在某物体_智能家居组件漫谈——人体传感器
  8. HTML怎么把文字分栏_JS将HTML生成PDF并下载
  9. ARM 之三 Keil uVision4、Keil uVision5、Keil C51同一系统下的共存
  10. 怎么知道电脑是32位还是64位_vnc 64位远程控制软件,你用的vnc 远程控制软件是32位还是64位?...