日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Document 工具类

發布時間:2023/12/16 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Document 工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、說明

Java 對本地HTML文件的讀取和寫入的工具類,可以用來修改靜態HTML的內容。

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: 文檔解析工具類* @author: 危錦輝 http://wjhsmart.vip* @create: 2019-09-02 11:26*/ public class DocumentUtil {/*** 傳入文件路徑 傳入參數Map 組合后獲取document* @param fileUrl 文件路徑* @param map 參數* @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);}/*** 傳入文件路徑 傳入參數Map 轉碼方式 獲取document* @param fileUrl 文件路徑* @param charsetName 編碼,如:UTF-8* @param map 參數* @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);}/*** 傳入文件 傳入參數 使用默認UTF-8* @param file 需要解析的文件* @param map 參數* @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 參數* @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;// 獲取元素并操作元素對象賦值for (Map.Entry<String, String> entry : map.entrySet()) {element = doc.getElementById(entry.getKey());if (element != null) {element.html(entry.getValue()==null?"":entry.getValue());}}// 把超文本語音html轉換為可擴展超文本語句xhtml // doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);return doc;}/*** 傳入到哪個文件路徑 傳入生成的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);}/*** 傳入到哪個文件路徑 傳入生成的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);}/*** 傳入到哪個文件 傳入生成的document* @param file* @param doc* @throws IOException*/public static void write(File file, Document doc) throws IOException {write(file, "UTF-8", doc);}/*** 將document內容寫入文件中* @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);// 設置輸出流OutputStreamWriter osw = new OutputStreamWriter(fos, charsetName);// 講doc寫入文件中osw.write(doc.html());osw.close();} }

?

總結

以上是生活随笔為你收集整理的Java Document 工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。