使用:
类.class.getClassLoader().getResource("*")).getPath();得到文件路径出现乱码: 原代码:
package com.cjl.jsoup; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Objects; public class JsoupDemo1 { public static void main(String[] args) { String path = Objects.requireNonNull(JsoupDemo1.class.getClassLoader().getResource("student.xml")).getPath(); try { Document document = Jsoup.parse(new File(path), "utf-8"); Elements elements = document.getElementsByTag("name"); System.out.println(elements.size()); } catch (Exception e) { e.printStackTrace(); } } }出现文件找不到的异常:
java.io.FileNotFoundException: C:\Users\24550\eclipse-workspace\IDEA测试练习\out\production\IDEA测试练习\student.xml (系统找不到指定的路径。) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at org.jsoup.helper.DataUtil.load(DataUtil.java:51) at org.jsoup.Jsoup.parse(Jsoup.java:103) at com.cjl.jsoup.JsoupDemo1.main(JsoupDemo1.java:17)对path进行修改:
path = java.net.URLDecoder.decode(path, "utf-8");URL的编码和解码的问题,地址用Encode编码要用与URLDecode进行解码。
修复后代码:
package com.cjl.jsoup; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import java.io.File; import java.util.Objects; public class JsoupDemo1 { public static void main(String[] args) { String path = Objects.requireNonNull(JsoupDemo1.class.getClassLoader().getResource("student.xml")).getPath(); try { path = java.net.URLDecoder.decode(path, "utf-8"); Document document = Jsoup.parse(new File(path), "utf-8"); Elements elements = document.getElementsByTag("name"); System.out.println(elements.size()); } catch (Exception e) { e.printStackTrace(); } } }