开发者社区> 问答> 正文

对应文件数里面有对应数,比如第5个文件里面打开是0005:报错

怎么批量生成有序列号的html。需要快速生成9999个html文件,对应文件数里面有对应数,比如第5个文件里面打开是0005:报错

文件0001.html  里面数字0001

       0002.html  里面数字0002

要0001-9999,9999个文件有没有办法批量生成.....感觉和快递面单差不多效果

展开
收起
kun坤 2020-06-14 09:55:48 472 0
1 条回答
写回答
取消 提交回答
  • 随便用某种语言,自己定义好模板,很容易实现吧

    ######推荐python######

    生成一个文件 1 毛钱,999.9 元帮你搞定。

    ######

    我只要500块,哈哈哈哈   

    ######模板引擎了解一下######容易实现… 问题是你打算做啥######

    java代码 

    package test;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;
    import java.util.HashMap;
    import java.util.Map;
    
    import freemarker.cache.FileTemplateLoader;
    import freemarker.cache.TemplateLoader;
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    
    public class Test {
        public static final Configuration config = new Configuration(Configuration.getVersion());
        static {
            TemplateLoader templateLoader;
            try {
                templateLoader = new FileTemplateLoader(new File("template.html"));
                config.setTemplateLoader(templateLoader);
            } catch (IOException e) {
            }
        }
    
        public static void main(String[] args) {
            try {
                Template t = config.getTemplate("template");
                Map<String, String> model = new HashMap<>();
                for (int i = 1; i <= 9999; i++) {
                    String fileName = "000" + String.valueOf(i);
                    String number = fileName.substring(fileName.length() - 4);
                    try (Writer out = new FileWriter("files/"+number+".html");) {
                        try {
                            model.put("number", number);
                            t.process(model, out);
                        } catch (TemplateException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    maven文件 

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>test</groupId>
        <artifactId>test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.29</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    模板文件 

    <html>
    <head>
    <title>test</title>
    </head>
    <body>
    ${number}
    </body>
    </html>

     

    ######你这个题看完c语言入门都能做######
    #循环生成0-9999数字
    for key in range(9999):
        #转换数字为4位长度的字符串插入到%s位置
        print("<html><p>%s</p></html>" % str(key).rjust(4, "0"))
    

    用字符串拼接,再保存到文件就行了,可以找基础教程通读几遍,就能熟练操作了.

    ######

    给我500, 让我来!

    2020-06-14 09:55:59
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载