导出数据的应用框架,通常对于具体的导出内容和格式是有要求的:
分成三部分,文件头、文件体、文件尾
文件头:分公司编号、导出数据的日期,对于文本格式,中间用逗号分离
文件体:表名称,然后分条描述数据。。。。
文件尾:输出人
不管是输出文本文件,还是输出XML文件,步骤基本一致
先拼接文件头的内容
然后拼接文件体的内容
在拼接文件尾的内容
最后把拼接好的内容输出去成为文件
Builder
package 建造者模式;
public abstract class Builder {
protected String id;
protected String date;
protected String TableName;
protected String data;
protected String Person;
public Builder(String id, String date, String tableName, String data,String person)
{
this.id = id;
this.date = date;
TableName = tableName;
this.data = data;
Person = person;
}
public abstract void BuildHead();
public abstract void BuildBody();
public abstract void BuildFoot();
}
TXT
package 建造者模式;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class TXT extends Builder {
public TXT(String id, String date, String tableName, String data,
String person) {
super(id, date, tableName, data, person);
}
private String str="";
@Override
public void BuildHead() {
str+=id+","+date+"\r\n";
}
@Override
public void BuildBody() {
str+=TableName+"\r\n";
str+=data+"\r\n";
}
@Override
public void BuildFoot() {
str+=Person+"\r\n";
}
public void Result() throws IOException{
File f1=new File("c:\\a.txt");
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(str);
bw.close();
}
}
XML
package 建造者模式;
import java.io.File;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class XML extends Builder {
private Document document;
private Element container;
public XML(String id, String date, String tableName, String data,
String person) throws ParserConfigurationException
{
super(id, date, tableName, data, person);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
document = db.newDocument();
}
@Override
public void BuildHead() {
document.setXmlStandalone(true);
container=document.createElement("Conatiner");
Element head=document.createElement("Head");
Element Id = document.createElement("Id");
Id.setTextContent(id);
Element Date = document.createElement("Date");
Date.setTextContent(date);
head.appendChild(Id);
head.appendChild(Date);
container.appendChild(head);
}
@Override
public void BuildBody() {
Element Body=document.createElement("body");
Element tname = document.createElement("TableName");
tname.setTextContent(TableName);
Element Data = document.createElement("Data");
Data.setTextContent(data);
Body.appendChild(Data);
Body.appendChild(tname);
container.appendChild(Body);
}
@Override
public void BuildFoot() {
Element foot=document.createElement("foot");
Element per = document.createElement("Person");
per.setTextContent(Person);
foot.appendChild(per);
container.appendChild(foot);
document.appendChild(container);
}
public void Result() throws TransformerException
{TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(new DOMSource(document), new StreamResult(new File("g:/shujv.xml")));
}
}
Director
package 建造者模式;
public class Director {
private Builder eb;
public Director(Builder eb) {
this.eb = eb;
}
public void Build(){
eb.BuildHead();
eb.BuildBody();
eb.BuildFoot();
}
}
Test
package 建造者模式;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
public class Test {
public static void main(String args[]) throws IOException, ParserConfigurationException, TransformerException{
TXT tb=new TXT("001","2020/4/16","数目","20","张三");
Director dr=new Director(tb);
dr.Build();
tb.Result();
XML ex=new XML("002","2020/4/17","价格","30","李四");
dr=new Director(ex);
dr.Build();
ex.Result();
}
}