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 {
// 解析模板文件爲 doc
Document 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();
}
}