public TxtBook findtxtUpload(String sortname, String name, String author,
String description, String buffer) {
try {
// 小说名包括后缀
String filename = name.concat(".txt");
String uploadPath = null;
// 创建SAXReader的对象
SAXReader sax = new SAXReader();
// 读入xml文件,并将其转换为Document类型
// Document doc = sax.read("WuxiaInfo.xml");// WuxiaInfo
Document doc = null;
if (SysConstants.SYS_WUXIA.equals(sortname)) {
doc = sax.read("WuxiaInfo.xml");
uploadPath = (SysConstants.SYS_WUXIAPATH).concat(
filename);
} else if (SysConstants.SYS_YANQING.equals(sortname)) {
doc = sax.read("YanqingInfo.xml");
uploadPath = (SysConstants.SYS_YANQINGPATH)
.concat(filename);
}
Element root = doc.getRootElement();
// 标示符,false代表上传的小说不存在,true代表上传的小说存在
boolean flag = false;
Iterator it = root.elementIterator();
// 循环查看每个元素的内容,判断上传的小说是否已经存在
while (it.hasNext()) {
Element el = (Element) it.next();
if (el.elementText("name").equals(name)) {
flag = true;
}
}
if (!flag) {// /如果没有重名的小说,上传小说
Element el = root.addElement("txtbook");
el.addElement("name").addText(name);
el.addElement("author").addText(author);
el.addElement("description").addText(description);
el.addElement("filename").addText(filename);
// // 将修改后的Document转换至硬盘
// // 美化格式,生成的代码有缩进
OutputFormat out = OutputFormat.createPrettyPrint();
XMLWriter writer = null;
if (SysConstants.SYS_WUXIA.equals(sortname)) {
writer = new XMLWriter(
new FileOutputStream("WuxiaInfo.xml"), out);
} else if (SysConstants.SYS_YANQING.equals(sortname)) {
writer = new XMLWriter(new FileOutputStream("Yanqing.xml"),
out);
}
writer.write(doc);
writer.close();
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(uploadPath, true));
BufferedWriter bw = new BufferedWriter(osw);
bw.write(buffer);
bw.close();
osw.close();
txtBook.setName(name);
txtBook.setAuthor(author);
txtBook.setAbsolutePath(description);
datas.setTxtBook(txtBook);
return txtBook;
} } catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}