Batch change the encoding method in meta information of HTML files

WBOY
Release: 2016-07-25 09:08:18
Original
1114 people have browsed it
Sometimes the encoding method of the html file is different from the encoding method specified in the meta information. You can use this code to fix it. This program relies on jsoup and commons-io packages

  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.io.Writer;
  5. import java.util.Iterator;
  6. import org.apache. commons.io.FileUtils;
  7. import org.jsoup.Jsoup;
  8. import org.jsoup.nodes.Document;
  9. import org.jsoup.nodes.Element;
  10. import org.jsoup.select.Elements;
  11. public class main {
  12. /**
  13. * @param args
  14. * @throws IOException
  15. */
  16. public static void main(String[] args) throws IOException {
  17. // TODO Auto-generated method stub
  18. File input = new File("C:\Users\jack\Desktop \New Folder\jdk-zh");
  19. Iterator it = FileUtils.iterateFiles(input, null, true);
  20. while (it.hasNext()) {
  21. File file = it.next();
  22. Document doc = Jsoup.parse(file, "gb2312");
  23. Elements content = doc.getElementsByAttributeValueStarting("content", "text/html;");
  24. for (Element meta : content) {
  25. meta.attr("content ", "text/html; charset=utf-8");
  26. System.out
  27. .println("Modify content--------" + file.getName() + "---");
  28. }
  29. FileUtils.writeStringToFile(file, doc.html(),"utf-8");
  30. }
  31. }
  32. }
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!