/**
* 插入单条模板记录
*
* @param vo 模板VO对象
* @return 若添加成功,返回新生成的主键对象OID
* @throws IOException 读写文件失败时会抛出此异常
*/
public OID insert(EwpTemplate ewpTemplateVO) throws Exception {
//模板VO保存到了数据库
getDao().insert(ewpTemplateVO);
//把模板内容保存到文件系统
String filepath = ewpTemplateVO.getFilepath(); //文件路径
String directory = ewpTemplateVO.getDirectory(); //文件目录
OutputStreamWriter fw = null;
BufferedWriter bw = null;
try {
// 如果目录不存在则创建目录,不创建目录直接创建文件会抛出FileNotFoundException
File dir = new File(directory);
if (!dir.exists()) {
dir.mkdirs();
}
// 保存模板内容文件
fw = new OutputStreamWriter(new FileOutputStream(new File(filepath)), "UTF-8");
bw = new BufferedWriter(fw);
bw.write(ewpTemplateVO.getTemplate_content());
} catch (IOException e) {
throw e;
} finally {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
}
return new OID(1);
}
/**
* 按UTF-8编码读取文件
*/
public void readFile() throws Exception{
String filepath = ".";
File file = new File(filepath);
if(file != null && file.exists()){
InputStreamReader reader = null;
BufferedReader br = null;
try{
reader = new InputStreamReader(new FileInputStream(file),"UTF-8");
br = new BufferedReader(reader);
StringBuffer sb = new StringBuffer();
String str = null;
do{
str = br.readLine();
if(str != null){
sb.append(str+"<br/>");
}
}while(str!=null);
System.out.println(str);
}finally{
if(br != null){
br.close();
}
if(reader != null){
reader.close();
}
}
}
}