package com.sun;

package com.sun;
/**
* @author 以一个文件为模板,个输出N相同模板的文章
* (1)将真实数据放入到集合中
* (2)读取模板,将模板信息放到一个字符串
* (3) 循环集合,在字符串查找符号,
* (4)将真实数据替换到符号位置
* (5)将替换后的字符串写入到一个新文件,注意文件名组成: yyyyMMddHHmmss.txt
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) throws Exception {
String news = null;
List<Essay> list=new ArrayList<Essay>();
for(int i=1;i<=10;i++){

Date date = null;
Essay e=new Essay("标题"+i,"张"+i, date);
list.add(e);
}
Iterator it=list.iterator();
while(it.hasNext()){
Essay e= (Essay)it.next();
String s=read();
String s1=s.replace("#title#", e.getTitle());
String s2=s1.replace("#date#", e.getDate());
news=s2.replace("#author#", e.getAuthor());
createFile(news );
}


}

//从模板中读出数据集并将它存到一个字符串中。
public static String read() throws IOException{
File f=new File("e.txt");
char[] ch=new char[1024];
FileReader fr=new FileReader(f);
int length=0;
String s=null;
while((length=fr.read(ch, 0, ch.length))!=-1){
s=new String(ch,0,length);

}
return s;
}
//创建一个文件存储数据
public static void createFile(String s) throws IOException{
//创建一个文件,并且以当前系统的时间作为文件名。
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmssSSS");
String date=sdf.format(d);
System.out.println(date);
File.createTempFile(date, ".txt", new File(""));
//想这个文件中写入数据。
File f=new File(date+".txt");
if(!f.exists()){
f.createNewFile();
}
byte[] ch=s.getBytes();;

FileOutputStream fos=new FileOutputStream(f);

fos.write(ch, 0, ch.length);
fos.flush();
fos.close();


}

}
//创建一个论文类。
class Essay{
private String title;
private String author;
private Date date;

public Essay(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}


public Essay() {
super();
}


public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getDate() {
Date date=new Date();
String d=date.toString();
return d;
}

public void setDate(Date date) {
this.date = date;
}



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值