简体   繁体   English

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

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

What I'm trying to achieve is a program that makes a file from the template called WaarschuwingsBriefTemplate.txt (WarningLetterTemplate).The method is called with a Klant (Customer) in its parantheses. 我要实现的是一个程序,该程序从名为WaarschuwingsBriefTemplate.txt(WarningLetterTemplate)的模板制作文件。该方法在其括号中用Klant(客户)调用。

Now when I call this method, it won't write any enters at all, even though there are enters in the Template and I'm trying to add enters in the method itself but it doesnt seem to work. 现在,当我调用此方法时,即使模板中有Enters,它也不会写任何Enters,并且我试图在方法本身中添加Enters,但它似乎不起作用。 Brief translation of the foreign words: 外来词的简要翻译:

NAAM = NAME NAAM = NAME

ADRES = ADDRESS 地址=地址

POSTCODE = ZIP CODE 邮政编码=邮政编码

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();
        }
}

` `

If your template is not too big and it's ok to read the file as whole to memory maybe try https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/text/StrSubstitutor.html instead of manual manipulation? 如果您的模板不是太大,可以将整个文件读取到内存中,请尝试https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ text / StrSubstitutor.html而不是手动操作?

Also use http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#lineSeparator-- instead of "\\n" 另外,请使用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