生成任意内容任意类型的文件
任何類型都可以,內(nèi)容也是,excel雖然也可以生成,但它會(huì)把所有內(nèi)容都放到第一個(gè),所以還是老實(shí)用一般方法吧。
?
這個(gè)有點(diǎn)麻煩,我們一步步來(lái)(可以先拉到最后看一眼配置文件會(huì)比較好理解)
第一個(gè)類:調(diào)用方法
public void test(){//參數(shù)一是需要放進(jìn)去的內(nèi)容,具體肯定不會(huì)這么短(根據(jù)模板多個(gè)的情況下改用list)//參數(shù)二是讀取配置文件的路徑insertContent("111", "D:/dev/workspace/lzpt/resource/"); }?
第二個(gè)類:基礎(chǔ)方法類
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/");}//上面調(diào)用的就是這個(gè)方法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>();// 實(shí)例化一個(gè)map//根據(jù)配置文件的參數(shù)可能有多個(gè),這里只有一個(gè)content,因?yàn)橹慌淞薱ontentmap.put("content", content);//取名字,隨便取吧String name = UUIDUtils.create();try {//第一個(gè)參數(shù)是內(nèi)容配置文件,可以配置多個(gè),多個(gè)的話,這段代碼復(fù)制粘貼幾遍就行了//第四個(gè)參數(shù)(就是那個(gè)有.exe的,是文件類型,什么后綴出來(lái)的就是什么類型的文件。exe當(dāng)然也可以生成,雖然根據(jù)內(nèi)容可能無(wú)法運(yùn)行(還是txt,doc這種觀賞類的比較適用)……)VelocityInfoOp.generatorCode("template.vm", map, this.sourcePath, name + ".exe", path);//沒(méi)什么意義,看一眼信息用的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;} }?
下一個(gè)類:配路徑用的
public static synchronized String getPropertyFromFile(String path, String filename, String key) {// getProperty獲取指定鍵指示的系統(tǒng)屬性//獲取路徑配置文件時(shí)的路徑,具體情況具體分析String paodingAnalysisPath = path + filename;// 定義一個(gè)輸入流InputStream in1 = null;// 定義一個(gè)類,資源包包含特定于語(yǔ)言環(huán)境的對(duì)象(需要特定語(yǔ)言環(huán)境的的資源時(shí),加載資源包內(nèi)的信息)ResourceBundle rb = null;try {in1 = new BufferedInputStream(new FileInputStream(paodingAnalysisPath));rb = new PropertyResourceBundle(in1);} catch (Exception e) {e.printStackTrace();}return rb.getString(key).trim();// 去掉空格,返回內(nèi)容 }?
最后一個(gè)類:
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(); //獲取內(nèi)容配置文件的路徑,同樣具體情況具體分析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("寫文件內(nèi)容操作出錯(cuò)");e.printStackTrace();}} }
?
最后放一下配置文件里面的內(nèi)容
template.config.properties里面:
author=SOMEONE
classPackage=com.test
codePath=/dev/workspace/test/src
就是些路徑,方便改,你也可以直接寫在類里面
?
template.vm里面:
${content}
這里說(shuō)明一下
如果配的是:
內(nèi)容為:${content}
前面map里面,content的內(nèi)容為111
文件內(nèi)容最終會(huì)是“內(nèi)容為:111”
參數(shù)可以是多個(gè),幾個(gè)參數(shù)map就放幾個(gè)值,鍵名與參數(shù)名一致就行
?
以上。
轉(zhuǎn)載于:https://www.cnblogs.com/IceBlueBrother/p/8423105.html
總結(jié)
以上是生活随笔為你收集整理的生成任意内容任意类型的文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 投影仪跟电脑怎么连接 投影仪和电脑怎么连
- 下一篇: 常州市金坛区南环二路大货车限行吗