生成任意内容任意类型的文件

本文介绍了一种使用Java实现的代码生成器,该生成器通过配置文件和模板自动生成特定格式的文件,如文本或可执行文件。文章详细展示了生成器的工作流程、涉及的类及其方法,并提供了配置文件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

任何类型都可以,内容也是,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,因为只配了content
map.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就放几个值,键名与参数名一致就行


以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值