痴书先生 2019-04-02 11:48 采纳率: 50%
浏览 2242
已结题

此处为啥一直报找不到文件的异常:java.io.FileNotFoundException: Template "contextxml.ftl" not found.

public class ExportToXmlUtil {

private Configuration configuration = null;

public ExportToXmlUtil(){
    configuration = new Configuration();

// configuration.setDefaultEncoding("UTF-8");
}
public static void main(String[] args) {
ExportToXmlUtil test = new ExportToXmlUtil();
test.createContextXml();
}

public void createContextXml(){
    Map<String,String> dataMap=new HashMap<>();
    getData(dataMap);
    configuration.setClassForTemplateLoading(this.getClass(), "classpath:/template");  //FTL文件所存在的位置,放在与java相同的包下

    Template t=null;
    try {
        t = configuration.getTemplate("contextxml.ftl"); //文件名
    } catch (IOException e) {
        e.printStackTrace();
    }
    File outFile = new File("D:/forK8sOutFile/context.xml");  //生成文件的路径
    Writer out = null;
    try {
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }

    try {
        t.process(dataMap, out);
    } catch (TemplateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
//这里赋值的时候需要注意,xml中需要的数据你必须提供给它,不然会报找不到某元素错的.
private Map<String,String> getData(Map<String, String> dataMap) {
    dataMap.put("name", "testName");
    dataMap.put("auth", "testAuth");
    return dataMap;
}

}

我的项目结构图随后附上

  • 写回答

3条回答 默认 最新

  • 人到中年就秃头 2019-04-02 12:07
    关注

    resources下的template是否有contextxml.ftl文件呢

    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度