我一个项目中用到的,里面的方法不是太通用,但是可以从里面找到一些有用的代码,以后慢慢添补更新:
FileUtil.xml
package com.novel.util; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader; /** * @author cy * * @date 2015年7月24日 上午8:38:38 * * @Description 关于文件的一些工具 */public class FileUtils { /** * 将文件中所有内容读取到字符串中 * * @param filePath * 文件路径 * @return 文件内容 */ public static String getStringFromFile(String filePath) { File file = new File(filePath) ; if(!file.exists()){ return "" ; } /** * 处理文件读取乱码问题 : * 只要判定两种常见的编码就可以了:GBK和UTF-8。由于中文Windows默认的编码是GBK,所以一般只要判定UTF-8编码格式。 *对于UTF-8编码格式的文本文件,其前3个字节的值就是-17、-69、-65 */ try{ byte[] firstThreeByte = new byte[3] ; InputStream in = new FileInputStream(file) ; in.read(firstThreeByte) ; in.close() ; String encoding = "" ; if(firstThreeByte[0] == -17 && firstThreeByte[1] == -16 && firstThreeByte[2] == -65){ encoding = "utf-8" ; }else{ encoding = "gbk" ; } InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding); Long filelength = file.length() / 2 ; // 该方法获取的是文件字节长度, //而我要创建的是char数组,char占两个字节, //byte一个字节,所以除以2表示的是该文件的字符长度 char[] filecontent = new char[filelength.intValue()] ; read.read(filecontent) ; return new String(filecontent) ; }catch(Exception e ){ e.printStackTrace(); return "" ; } } /** * 将字符串写入文件 * * @param content * 字符串内容 * @param filePath * 文件路径 * @throws IOException */ public static void writeStringToFile(String content, String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { file.createNewFile(); } FileOutputStream out = new FileOutputStream(file); out.write(content.getBytes()); out.close(); } /** * 删除指定的文件 * @param filePath文件路径 */ public static void deleteFile(String filePath ) { File file = new File(filePath) ; if(file.exists()){ file.delete() ; } }}
登录后复制
XmlUtil.java
package com.novel.util; import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.TransformerFactoryConfigurationError;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;import org.xml.sax.SAXException; import com.novel.entity.Novel;import com.novel.entity.User; /** * @author cy * * @date 2015年7月23日 下午3:19:06 * * @Description 关于xml的操作 */public class XmlUtil { /** * 目标xml为 config/users.xml * * @param user * 将要被写入xml的User对象 * @return 是否成功 */ public static boolean writeUserToXml(User user) { try { Document doc = getDocumentFromXml("config/users.xml"); Element newUserElement = doc.createElement("user"); Element newUsernameElement = doc.createElement("name"); Text nameTextNode = doc.createTextNode("nameValue"); nameTextNode.setNodeValue(user.getName()); newUsernameElement.appendChild(nameTextNode); Element newUserPwdElement = doc.createElement("pwd"); Text pwdTextNode = doc.createTextNode("pwdValue"); pwdTextNode.setNodeValue(user.getName()); newUserPwdElement.appendChild(pwdTextNode); newUserElement.appendChild(newUsernameElement); newUserElement.appendChild(newUserPwdElement); Element usersElement = (Element) doc.getElementsByTagName("users") .item(0); usersElement.appendChild(newUserElement); writeDocumentToFile(doc, "config/users.xml"); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * * @param doc * XML中的Document对象 * @param filePath * 输出的文件路径 * @throws TransformerFactoryConfigurationError * @throws TransformerConfigurationException * @throws TransformerException */ private static void writeDocumentToFile(Document doc, String filePath) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { // 写入到硬盘 TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); /** 编码 */ transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(filePath)); transformer.transform(source, result); } /** * 加载config/users.xml中用户信息到对象中 * * @return 加载后的对象 */ public static Map initUser() { InitUser.users = new HashMap(); try { Document doc = getDocumentFromXml("config/users.xml"); NodeList usersNodeList = doc.getElementsByTagName("user"); for (int i = 0; i getNovelListFromXml(String filePath) throws SAXException, IOException, ParserConfigurationException { List novelList = new ArrayList(); Document doc = getDocumentFromXml(filePath); NodeList novels = doc.getElementsByTagName("novel"); for (int i = 0; i
登录后复制
以上就是xml,文件操作功能类的示例代码详解的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2418882.html