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

来源:互联网 发布:js定义map 编辑:程序博客网 时间:2024/05/12 09:43

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


以上。

原创粉丝点击