Java Document 工具类
生活随笔
收集整理的這篇文章主要介紹了
Java Document 工具类
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、說(shuō)明
Java 對(duì)本地HTML文件的讀取和寫(xiě)入的工具類(lèi),可以用來(lái)修改靜態(tài)HTML的內(nèi)容。
2、Maven包
需要引入jsoup包
<dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.3</version><scope>compile</scope> </dependency>3、code
package top.zywork.common;import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element;import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Map;/*** @program: zywork-common* @description: 文檔解析工具類(lèi)* @author: 危錦輝 http://wjhsmart.vip* @create: 2019-09-02 11:26*/ public class DocumentUtil {/*** 傳入文件路徑 傳入?yún)?shù)Map 組合后獲取document* @param fileUrl 文件路徑* @param map 參數(shù)* @return* @throws IOException*/public static Document getDocument(String fileUrl, Map<String, String> map) throws IOException {File file = new File(fileUrl);return getDocument(file, "UTF-8", map);}/*** 傳入文件路徑 傳入?yún)?shù)Map 轉(zhuǎn)碼方式 獲取document* @param fileUrl 文件路徑* @param charsetName 編碼,如:UTF-8* @param map 參數(shù)* @return* @throws IOException*/public static Document getDocument(String fileUrl, String charsetName, Map<String, String> map) throws IOException {File file = new File(fileUrl);return getDocument(file, charsetName, map);}/*** 傳入文件 傳入?yún)?shù) 使用默認(rèn)UTF-8* @param file 需要解析的文件* @param map 參數(shù)* @return* @throws IOException*/public static Document getDocument(File file, Map<String, String> map) throws IOException {return getDocument(file, "UTF-8", map);}/*** 讀取文件到Document* @param file 文件* @param charsetName 編碼* @param map 參數(shù)* @return* @throws IOException*/public static Document getDocument(File file, String charsetName, Map<String, String> map) throws IOException {// 解析模板文件為 docDocument doc = Jsoup.parse(file, charsetName);Element element = null;// 獲取元素并操作元素對(duì)象賦值for (Map.Entry<String, String> entry : map.entrySet()) {element = doc.getElementById(entry.getKey());if (element != null) {element.html(entry.getValue()==null?"":entry.getValue());}}// 把超文本語(yǔ)音html轉(zhuǎn)換為可擴(kuò)展超文本語(yǔ)句xhtml // doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);return doc;}/*** 傳入到哪個(gè)文件路徑 傳入生成的document* @param fileUrl 需要生成文件的路徑* @param doc 生成的文檔* @throws IOException*/public static void write(String fileUrl, Document doc) throws IOException {File file = new File(fileUrl);write(file, "UTF-8", doc);}/*** 傳入到哪個(gè)文件路徑 傳入生成的document* @param fileUrl 文件路徑* @param charsetName 編碼* @param doc 文檔* @throws IOException*/public static void write(String fileUrl,String charsetName, Document doc) throws IOException {File file = new File(fileUrl);write(file, charsetName, doc);}/*** 傳入到哪個(gè)文件 傳入生成的document* @param file* @param doc* @throws IOException*/public static void write(File file, Document doc) throws IOException {write(file, "UTF-8", doc);}/*** 將document內(nèi)容寫(xiě)入文件中* @param file* @param charsetName* @param doc* @throws IOException*/public static void write(File file, String charsetName, Document doc) throws IOException {if (!file.exists()) {file.createNewFile();}FileOutputStream fos = new FileOutputStream(file, false);// 設(shè)置輸出流OutputStreamWriter osw = new OutputStreamWriter(fos, charsetName);// 講doc寫(xiě)入文件中osw.write(doc.html());osw.close();} }?
總結(jié)
以上是生活随笔為你收集整理的Java Document 工具类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 算法入侵,不如拥抱、打造更好的个性化推荐
- 下一篇: bilibiliC++25程序流程结构-