io流

package My.Dom;

public class MovieBean {

private String name,actors,showtime,filmlong,area,type,writer,chinesename;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getActors() {
return actors;
}

public void setActors(String actors) {
this.actors = actors;
}

public String getShowtime() {
return showtime;
}

public void setShowtime(String showtime) {
this.showtime = showtime;
}

public String getFilmlong() {
return filmlong;
}

public void setFilmlong(String filmlong) {
this.filmlong = filmlong;
}

public String getArea() {
return area;
}

public void setArea(String area) {
this.area = area;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getWriter() {
return writer;
}

public void setWriter(String writer) {
this.writer = writer;
}

public String getChinesename() {
return chinesename;
}

public void setChinesename(String chinesename) {
this.chinesename = chinesename;
}



}

------------------------------------------------------------------------
package My.CreateFileUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;

public class CreateFileUtil {
  /**
  * 创建目录
  * @param destDirName 目标目录名
  * @return 目录创建成功放回true,否则返回false
  */
  public static boolean createDir(String destDirName) {
  File dir = new File(destDirName);
  if (dir.exists()) {
  System.out.println("创建目录" + destDirName + "失败,目标目录已存在!");
  return false;
  }
  if (!destDirName.endsWith(File.separator)) {
  destDirName = destDirName + File.separator;
  }
  // 创建目标目录
  if (dir.mkdirs()) {
  System.out.println("创建目录" + destDirName + "成功!");
  return true;
  } else {
  System.out.println("创建目录" + destDirName + "失败!");
  return false;
  }
  }
  public static boolean createFile(String destFileName) {
  File file = new File(destFileName);
  if (file.exists()) {
  System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
  return false;
  }
  if (destFileName.endsWith(File.separator)) {
  System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");
  return false;
  }
  // 判断目标文件所在的目录是否存在
  if (!file.getParentFile().exists()) {
  // 如果目标文件所在的文件夹不存在,则创建父文件夹
  System.out.println("目标文件所在目录不存在,准备创建它!");
  if (!file.getParentFile().mkdirs()) {
  System.out.println("创建目标文件所在的目录失败!");
  return false;
  }
  }
  // 创建目标文件
  try {
  if (file.createNewFile()) {
  System.out.println("创建单个文件" + destFileName + "成功!");
  return true;
  } else {
  System.out.println("创建单个文件" + destFileName + "失败!");
  return false;
  }
  } catch (IOException e) {
  e.printStackTrace();
  System.out
  .println("创建单个文件" + destFileName + "失败!" + e.getMessage());
  return false;
  }
  }
  
  /**
  * 以字符为单位写文件。
  * @param fileName 文件名
  */
  public static void writeFileByChars(String fileName,String content){
  File file = new File(fileName);
  Writer writer = null;
  try {
  //打开文件输出流
  writer = new OutputStreamWriter(new FileOutputStream(file));
  writer.write(content);
  System.out.println("写文件" + file.getAbsolutePath() + "成功!");
  } catch (IOException e){
  System.out.println("写文件" + file.getAbsolutePath() + "失败!");
  e.printStackTrace();
  } finally {
  if (writer != null){
  try {
  //关闭输出文件流
  writer.close();
  } catch (IOException e1) {
  }
  }
  }
  }
  
}

------------------------------------------------------------------------
package My.Dom;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import My.CreateFileUtil.CreateFileUtil;

public class ParseXML {

public static String rootNodeName;

public static List readXMLFile(String inFile, String dirName)
throws Exception {
// 得到DOM解析器的工厂实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
// 从DOM工厂获得DOM解析器
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
System.err.println(pce);
return null;
}
Document doc = null;
try {
// 解析XML文档的输入流,得到一个Document
doc = db.parse(inFile);
// 对document对象调用normalize(),可以去掉xml文档中作为格式化内容的空白,
// 避免了这些空白映射在dom树中成为不必要的text node对象。
// 否则你得到的dom树可能并不是你所想象的那样。
// 特别是在输出的时候,这个normalize()更为有用。
doc.normalize();
} catch (DOMException dom) {
System.err.println(dom.getMessage());
return null;
} catch (IOException ioe) {
System.err.println(ioe);
return null;
}
List movieBeans = new ArrayList();
MovieBean movieBean = null;
// 得到XML文档的根节点“Movie”
Element root = doc.getDocumentElement();

rootNodeName = root.getNodeName();

CreateFileUtil.createDir(dirName + "/" + root.getNodeName());

NodeList nl = root.getChildNodes();

for (int i = 0; i < nl.getLength(); i++) {
Node nd = nl.item(i);
// 创建一个movie的Bean实例
movieBean = new MovieBean();
if (nd.getNodeType() == Node.ELEMENT_NODE) {
movieBean.setName(nd.getAttributes().getNamedItem("name")
.getNodeValue());
}

for (Node node = (Node) nd.getChildNodes(); node != null; node = node
.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals("actors")) {
movieBean
.setActors(node.getFirstChild().getNodeValue());
}
if (node.getNodeName().equals("showtime")) {
movieBean.setShowtime(node.getFirstChild()
.getNodeValue());
}
if (node.getNodeName().equals("filmlong")) {
movieBean.setFilmlong(node.getFirstChild()
.getNodeValue());
}
if (node.getNodeName().equals("area")) {
movieBean.setArea(node.getFirstChild().getNodeValue());
}
if (node.getNodeName().equals("type")) {
movieBean.setType(node.getFirstChild().getNodeValue());
}
if (node.getNodeName().equals("writer")) {
movieBean
.setWriter(node.getFirstChild().getNodeValue());
}
if (node.getNodeName().equals("chinesename")) {
movieBean.setChinesename(node.getFirstChild()
.getNodeValue());
}
}
}
movieBeans.add(movieBean);
}
return movieBeans;
}

// destFileName 路径名
public static void parseList(List movieList, String destFileName) {
String fileName = null;
// JDK5.0的新for循环方法
for (Iterator iterator = movieList.iterator(); iterator.hasNext();) {
MovieBean mb = (MovieBean) iterator.next();
// 使用已经设置好了的元素
String name = mb.getName();
String actors = mb.getActors();
String showtime = mb.getShowtime();
String filmlong = mb.getFilmlong();
String area = mb.getArea();
String type = mb.getType();
String writer = mb.getWriter();
String chinesename = mb.getChinesename();
fileName = name + "_" + actors.substring(0, 7) + "_" + "...";
System.out.println("fileName = " + fileName);
// 创建XX目录下的txt文本
CreateFileUtil.createFile(destFileName + "/" + rootNodeName + "/"
+ fileName + ".txt");
StringBuffer sbf = new StringBuffer();
sbf.append("<movie name=" + "\"" + name + "\"" + ">" + "\n");
sbf.append("<actors>" + actors + "</actors>" + "\n");
sbf.append("<showtime>" + showtime + "</showtime>" + "\n");
sbf.append("<filmlong>" + filmlong + "</filmlong>" + "\n");
sbf.append("<area>" + area + "</area>" + "\n");
sbf.append("<type>" + type + "</type>" + "\n");
sbf.append("<writer>" + writer + "</writer>" + "\n");
sbf.append("<chinesename>" + chinesename + "</chinesename>" + "\n");
sbf.append("</movie>");
CreateFileUtil.writeFileByChars(destFileName + "/" + rootNodeName
+ "/" + fileName + ".txt", sbf.toString());
}
}
}



------------------------------------------------------------------------
package My.Client;

import java.util.List;

import My.Dom.ParseXML;

public class Client {

public static void main(String[] args){
  String inFileName = "No1.xml";
  String dirName = "E:/Team.1/RSSParser";
List movieBeans;
try {
movieBeans = ParseXML.readXMLFile(inFileName, dirName);
ParseXML.parseList(movieBeans, dirName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

------------------------------------------------------------------------

运行结果:
创建目录E:/Team.1/RSSParser/movies\成功!
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.CharacterDataImpl$1
at My.Dom.ParseXML.readXMLFile(ParseXML.java:70)
at My.Client.Client.main(Client.java:14)

你可能感兴趣的:(IO流)