繁体   English   中英

Java-从具有布局的模板制作文件?

[英]Java- Making a File from template with layout?

我要实现的是一个程序,该程序从名为WaarschuwingsBriefTemplate.txt(WarningLetterTemplate)的模板制作文件。该方法在其括号中用Klant(客户)调用。

现在,当我调用此方法时,即使模板中有Enters,它也不会写任何Enters,并且我试图在方法本身中添加Enters,但它似乎不起作用。 外来词的简要翻译:

NAAM = NAME

地址=地址

邮政编码=邮政编码

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class FileMaker {
        public FileMaker(){
        }
        public void maakWaarschuwingsBrief(Klant k) throws IOException{
                File file = new File("WaarschuwingsBriefTemplate.txt");
                String newFile = "";

                try{
                        Scanner sc = new Scanner(file);
                        while(sc.hasNextLine()){
                                String line = sc.nextLine();
                                if(line.contains("--NAAM--")){
                                        line = line.replace("--NAAM--", k.getNaam())+"\n";
                                }
                                if(line.contains("--ADRES--")){
                                        line = line.replace("--ADRES--", k.getAdres())+"\n";
                                }
                                if(line.contains("--POSTCODE--")){
                                        line = line.replace("--POSTCODE--", k.getPostcode())+"\n";
                                }
                                newFile += line + "\n";

                        }
                        sc.close();
                }catch(FileNotFoundException  e){
                        e.printStackTrace();
                }
                File file2 = new File(k.getNaam().replaceAll("\\s","")+".txt");
                if(!file2.exists()){
                        file2.createNewFile();
                }
                FileWriter fw = new FileWriter(file2.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(newFile);
                bw.close();
        }
}

`

如果您的模板不是太大,可以将整个文件读取到内存中,请尝试https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ text / StrSubstitutor.html而不是手动操作?

另外,请使用http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#lineSeparator--代替“ \\ n”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM