UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务,IETF RFC 768是UDP的正式规范,本文代码采用了UDP协议的转发器。
Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页。最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度为何等),以便搜索引擎可以更加智能地抓取网站。
XML标签,具体参数含义如下:
changefreq:页面内容更新频率。 lastmod:页面最后修改时间 loc:页面永久链接地址 priority:相对于其他页面的优先权 url:相对于前4个标签的父标签 urlset:相对于前5个标签的父标签
废话不多说,JAVA 读写 sitemap.xml 文件具体代码如下:
package com.yoodb; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.RandomAccessFile; public class ReadWriteFile { private static String path = "E://sitemap.xml"; private static File fileName = new File(path); private static String readStr = ""; /** * @author www.yoodb.com * 创建文本文件 * @throws IOException */ public static Boolean creatTxtFile() throws IOException{ if (!fileName.exists()) { fileName.createNewFile(); return true; }else{ return false; } } /** * @author www.yoodb.com * 读取文本文件 * @throws IOException */ public static String readTxtFile(){ String read; FileReader fileread; BufferedReader bufread; try { fileread = new FileReader(fileName); bufread = new BufferedReader(fileread); try { while ((read = bufread.readLine()) != null) { readStr = readStr + read+ "\r\n"; } bufread.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return readStr; } /** * @author www.yoodb.com * 将文件中指定内容的第一行替换为其它内容 * @param oldStr 查找内容 * @param replaceStr 替换内容 */ public static void replaceTxtByStr(String oldStr,String replaceStr) { String temp = ""; try { File file = new File(path); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); StringBuffer buf = new StringBuffer(); // 保存该行前面的内容 while( (temp = br.readLine()) != null && !temp.equals(oldStr)) { buf = buf.append(temp); //System.getProperty("line.separator") 平台下行与行之间的分隔符相当于"" buf = buf.append(System.getProperty("line.separator")); } buf = buf.append(replaceStr); while ((temp = br.readLine()) != null) { buf = buf.append(System.getProperty("line.separator")); buf = buf.append(temp); } br.close(); FileOutputStream fos = new FileOutputStream(file); PrintWriter pw = new PrintWriter(fos); pw.write(buf.toString().toCharArray()); pw.flush(); pw.close(); } catch (IOException e) { e.printStackTrace(); } } /** * @author www.yoodb.com * 读取本地文件内容并增加新内容文件 * @throws IOException */ public static void writeTxtFile(String newStr){ String filein = newStr + "\r\n" + readStr + "\r\n"; RandomAccessFile ra = null; try { ra = new RandomAccessFile(fileName, "rw"); ra.write(filein.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (ra != null) { try { ra.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
为了保留传入的数据,在 sitemap.xml 原文件内容基础上增加新的内容,具体代码如下:
package com.yoodb; public class TestMain { public static void main(String[] args){ StringBuffer buf = new StringBuffer(); buf.append(" <url>\r\n"); buf.append(" <loc>http://blog.yoodb.com/yoodb/article/detail/224</loc>\r\n"); buf.append(" <priority>0.5</priority>\r\n"); buf.append(" <lastmod>2015-09-28</lastmod>\r\n"); buf.append(" <changefreq>daily</changefreq>\r\n"); buf.append(" </url>\r\n"); ReadWriteFile.replaceTxtByStr("</urlset>", buf.toString() + "</urlset>"); } }