package readFile;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* 文件读写工具类。
*/
public class FileIO {
/**
* 读取文件。
* @param filePath 文件路径
* @return 文件内容字符串
*/
public String readFile(String filePath) {
Reader fr = null;
StringBuffer sb = new StringBuffer();
// 1、读取模板文件内容到StringBuffer
try {
fr = new FileReader(filePath);
char ch[] = new char[1024];
int length = 0;
length = fr.read(ch);
while (length != -1) {
sb.append(ch);
length = fr.read(ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fr)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 写文件。
* @param filePath 文件路径
* @param str 要写入文件的内容
*/
public void writeFile(String filePath, String str) {
Writer fw = null;
try {
fw = new FileWriter(filePath);
fw.write(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fw)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
InfoVo 代码
package readFile;
public class InfoVo {
private Integer count;
private Integer offset;
private String title;
private String zengwen;
public String getZengwen() {
return zengwen;
}
public void setZengwen(String zengwen) {
this.zengwen = ""+zengwen+"";
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getOffset() {
return offset;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title =""+ title+"
";
}
}
test 代码
package readFile;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class test {
public List readFile(String path) throws FileNotFoundException {
FileReader fr =new FileReader(path);
BufferedReader br= new BufferedReader(fr);
Listlist =new ArrayList();//存放小说章节标题
Listtxtlist =new ArrayList();//存放小说内容
StringBuffer sb =null;
String lineTxt = null;
int offset = 0; //章节所在行数
int count = 1; //章节数
InfoVo infoVo;
try {
while ((lineTxt=br.readLine())!=null) {
infoVo = new InfoVo();
offset++;
if (lineTxt.contains("第") && lineTxt.contains("章")&&(lineTxt.length()<20)) {
infoVo.setCount(count);
infoVo.setOffset(offset);
infoVo.setTitle(lineTxt);
list.add(infoVo);
count++;
}
txtlist.add(lineTxt);
}
for (int i = 0; i <list.size(); i++) {
sb=new StringBuffer();
if((i+1) <list.size()) {
for (int j = list.get(i).getOffset(); j <=(list.get(i+1).getOffset()-2); j++) {
sb.append(""+txtlist.get(j)+"
"+"\n");
}
list.get(i).setZengwen(sb.toString());
}
if((i+1) ==list.size()) {
for (int j = list.get(i).getOffset(); j <offset; j++) {
sb.append(""+txtlist.get(j)+"
"+"\n");
}
list.get(i).setZengwen(sb.toString());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public static void main(String[] args) {
//1、读取模板文件内容,返回文件内容字符串
FileIO fileio=new FileIO();
String templatestr=fileio.readFile("C:\\Users\\赵\\Documents\\eclipse\\Projects\\java\\readFile\\src\\readFile\\news.template");
text text =new text();
String path ="C:\\Users\\赵\\Desktop\\小说资源\\秦吏.txt";
try {
List list= text.readFile(path);
// for (InfoVo infoVo : list) {
// System.out.println(infoVo.getTitle());
// System.out.println(infoVo.getZengwen());
// }
//3、替换模板文件,为每一条新闻创建一个HTML文件来显示其信息
for(int i=0;i<2;i++){
//3.1、获取一章小说
InfoVo info=list.get(i);
//3.2、使用该条新闻信息替换对应占位符
String replacestr=new String();
replacestr=templatestr;
replacestr = replacestr.replace("{mgs}", info.getZengwen());
//3.3、为该章小说生成HTML文件
String filePath="C:\\Users\\赵\\Documents\\html\\news"+i+".html";
fileio.writeFile(filePath, replacestr);
}
System.out.println("创建成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
模板
{mgs}