重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
/**
安义ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
xml中是不能写java代码的。
jsp中之所以能够写java代码,是因为jsp在返回给客户端的时候,会经由jsp解析器进行解析,执行,最终返回给客户端,而xml文件是不会出现这种操作的。
如果想使用代码片段的话,建议你使用模版来进行处理。
关于模版的技术,现在比较有名的就是velocity开源项目,你可以上网查询一些,如果有兴趣可以私下交流,这儿说不清楚
用java自带的就可以,有问题可以问我
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//构造
public XMLUtil(String name) throws ParserConfigurationException {
filename = name;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
/**
* 保存到文件
*/
public void toSave() {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
} catch (TransformerException mye) {
mye.printStackTrace();
} catch (IOException exp) {
exp.printStackTrace();
}
}
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.w3c.dom.*;import org.xml.sax.SAXException;import javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.*;import javax.xml.xpath.*;public class Test { public static void main(String[] args) { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); Element theBook=null, theElem=null, root=null; try { factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db=factory.newDocumentBuilder(); Document xmldoc=db.parse(new File("Test1.xml")); root=xmldoc.getDocumentElement(); theBook=(Element) selectSingleNode("/books/book[name='哈里波特']", root); System.out.println("--- 查询找《哈里波特》 ----"); Element nameNode=(Element)theBook.getElementsByTagName("price").item(0); String name=nameNode.getFirstChild().getNodeValue(); System.out.println(name); output(theBook); System.out.println("=============selectSingleNode(books/book[name='哈里波特'], root)=================="); //--- 新建一本书开始 ---- theBook=xmldoc.createElement("book"); theElem=xmldoc.createElement("name"); theElem.setTextContent("新书"); theBook.appendChild(theElem); theElem=xmldoc.createElement("price"); theElem.setTextContent("20"); theBook.appendChild(theElem); theElem=xmldoc.createElement("memo"); theElem.setTextContent("新书的更好看。"); theBook.appendChild(theElem); root.appendChild(theBook); System.out.println("--- 新建一本书开始 ----"); output(xmldoc); System.out.println("=============================="); //--- 新建一本书完成 ---- //--- 下面对《哈里波特》做一些修改。 ---- //--- 查询找《哈里波特》---- //--- 此时修改这本书的价格 ----- theBook.getElementsByTagName("price").item(0).setTextContent("15");//getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,getElementsByTagName("price")相当于xpath的".//price"。 System.out.println("--- 此时修改这本书的价格 ----"); output(theBook); //--- 另外还想加一个属性id,值为B01 ---- theBook.setAttribute("id", "B01"); System.out.println("--- 另外还想加一个属性id,值为B01 ----"); output(theBook); //--- 对《哈里波特》修改完成。 ---- //--- 要用id属性删除《三国演义》这本书 ---- theBook=(Element) selectSingleNode("/books/book[@id='B02']", root); System.out.println("--- 要用id属性删除《三国演义》这本书 ----"); output(theBook); theBook.getParentNode().removeChild(theBook); System.out.println("--- 删除后的XML ----"); output(xmldoc); //--- 再将所有价格低于10的书删除 ---- NodeList someBooks=selectNodes("/books/book[price10]", root); System.out.println("--- 再将所有价格低于10的书删除 ---"); System.out.println("--- 符合条件的书有 "+someBooks.getLength()+"本。 ---"); for(int i=0;isomeBooks.getLength();i++) { someBooks.item(i).getParentNode().removeChild(someBooks.item(i)); } output(xmldoc); saveXml("Test1_Edited.xml", xmldoc); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void output(Node node) {//将node的XML字符串输出到控制台 TransformerFactory transFactory=TransformerFactory.newInstance(); try { Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("encoding", "gb2312"); transformer.setOutputProperty("indent", "yes"); DOMSource source=new DOMSource(); source.setNode(node); StreamResult result=new StreamResult(); result.setOutputStream(System.out); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } public static Node selectSingleNode(String express, Object source) {//查找节点,并返回第一个符合条件节点 Node result=null; XPathFactory xpathFactory=XPathFactory.newInstance(); XPath xpath=xpathFactory.newXPath(); try { result=(Node) xpath.evaluate(express, source, XPathConstants.NODE); } catch (XPathExpressionException e) { e.printStackTrace(); } return result; } public static NodeList selectNodes(String express, Object source) {//查找节点,返回符合条件的节点集。 NodeList result=null; XPathFactory xpathFactory=XPathFactory.newInstance(); XPath xpath=xpathFactory.newXPath(); try { result=(NodeList) xpath.evaluate(express, source, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } return result; } public static void saveXml(String fileName, Document doc) {//将Document输出到文件 TransformerFactory transFactory=TransformerFactory.newInstance(); try { Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); DOMSource source=new DOMSource(); source.setNode(doc); StreamResult result=new StreamResult(); result.setOutputStream(new FileOutputStream(fileName)); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }} XML:?xml version="1.0" encoding="GBK"?booksbookname哈里波特/nameprice10/pricememo这是一本很好看的书。/memo/bookbook id="B02"name三国演义/nameprice10/pricememo四大名著之一。/memo/bookbook id="B03"name水浒/nameprice6/pricememo四大名著之一。/memo/bookbook id="B04"name红楼/nameprice5/pricememo四大名著之一。/memo/book/books