Hướng dẫn how to read html file in java - cách đọc file html trong java

Như Jean đã đề cập, sử dụng StringBuilder thay vì += sẽ tốt hơn. Nhưng nếu bạn đang tìm kiếm một cái gì đó đơn giản hơn, ổi, ioutils và jsoup đều là những lựa chọn tốt.

Ví dụ với ổi:

String content = Files.asCharSource[new File["/path/to/mypage.html"], StandardCharsets.UTF_8].read[];

Ví dụ với Ioutils:

InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }

Ví dụ với JSOUP:

String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];

hoặc

String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];

NOTES:

Files.readLines[]Files.toString[]

Những thứ này hiện không được dùng để phát hành Guava phiên bản 22.0 [ngày 22 tháng 5 năm 2017].

InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
0 nên được sử dụng thay thế như đã thấy trong ví dụ trên. [Phiên bản 22.0 Diễn xuất Diffs]as seen in the example above. [version 22.0 release diffs]

InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
1 và
InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
2

Không dùng nữa kể từ Apache Commons-io phiên bản 2.5 [ngày 6 tháng 5 năm 2016].

InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
3 bây giờ nên được thông qua
InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
4 và
InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
5 như đã thấy trong ví dụ trên. Nên sử dụng
InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
6 của Java 7 thay vì
InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
7 như đã thấy trong ví dụ trên. [Charsets không dùng nữa.utf_8]and the
InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
5 as seen in the example above. Java 7's
InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
6 should be used instead of
InputStream in = new URL["/path/to/mypage.html"].openStream[];
String content;

try {
   content = IOUtils.toString[in, StandardCharsets.UTF_8];
 } finally {
   IOUtils.closeQuietly[in];
 }
7 as seen in the example above. [deprecated Charsets.UTF_8]

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Approaches:

    1. Sử dụng Filereader
    2. Sử dụng url.openstream []

    Cách tiếp cận 1: Thư viện có tên Trình làm phim cung cấp cách đọc bất kỳ tệp nào bất kể bất kỳ tiện ích mở rộng nào. Cách để nối các dòng HTML vào trình tạo chuỗi như sau:The library called the FileReader which provides the way to read any File irrespective of any Extension. The way to append the HTML lines to the String Builder is as follows:

    1. Sử dụng trình filereader để đọc tệp từ thư mục nguồn và xa hơn nữa
    2. Nối mỗi dòng vào trình xây dựng chuỗi.
    3. Khi không còn nội dung nào trong tài liệu HTML thì hãy đóng tệp mở bằng hàm br.close [].br.close[].
    4. In ra chuỗi.

    Implementation:

    Java

    InputStream in = new URL["/path/to/mypage.html"].openStream[];
    String content;
    
    try {
       content = IOUtils.toString[in, StandardCharsets.UTF_8];
     } finally {
       IOUtils.closeQuietly[in];
     }
    
    8
    InputStream in = new URL["/path/to/mypage.html"].openStream[];
    String content;
    
    try {
       content = IOUtils.toString[in, StandardCharsets.UTF_8];
     } finally {
       IOUtils.closeQuietly[in];
     }
    
    9

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    0
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    1
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    2

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    3
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    0
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    5
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    6
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    7

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    9
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    0

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    3
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    2

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    4
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    5
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    6

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    8
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    5
    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    0

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1
    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    2
    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    3

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8
    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    5
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    2

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1
    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    8
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    5
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    0

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    2

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    4
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    55____56
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    7

    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    8
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    9

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1StringBuilder1

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1StringBuilder3

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1StringBuilder5

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1StringBuilder7

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8StringBuilder1

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8+=1 +=2

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1+=4

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8StringBuilder1

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    3StringBuilder1

    StringBuilder1

    Output:  

    Cách tiếp cận 2: Sử dụng url.openstream []Using the Url.openStream[]

    • Gọi hàm URL.openStream [] khởi tạo TCPConnection mới đến máy chủ mà URL cung cấp cho nó.url.openStream[]function that initiates the new TCPconnection to the Server that the URL provides it to.
    • Bây giờ, HTTP nhận được yêu cầu được gửi đến kết nối sau khi máy chủ gửi lại phản hồi HTTP chứa thông tin vào đó.
    • Thông tin đó ở dạng byte thì thông tin đó được đọc bằng phương thức inputStreamReader [] và openStream [] trả lại dữ liệu cho chương trình.
    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    • Đầu tiên, chúng tôi mở URL bằng TheopenStream [] để tìm nạp thông tin. Thông tin được chứa trong URL dưới dạng byte nếu kết nối hoàn toàn ổn [có nghĩa là hiển thị 200] sau đó yêu cầu HTTP để tìm nạp nội dung.openStream[]to fetch the information. The information is contained in the URL in the form of bytes if the connection is all OK [means is shows 200] then HTTP request to theURLTo fetch the content.
    • Sau đó, thông tin được thu thập dưới dạng byte bằng cách sử dụng theInputStreamReader []inputStreamReader[]
    • Bây giờ vòng lặp được chạy để in thông tin vì nhu cầu là in thông tin trong bảng điều khiển.
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }

    Implementation:  

    Java

    InputStream in = new URL["/path/to/mypage.html"].openStream[];
    String content;
    
    try {
       content = IOUtils.toString[in, StandardCharsets.UTF_8];
     } finally {
       IOUtils.closeQuietly[in];
     }
    
    8
    InputStream in = new URL["/path/to/mypage.html"].openStream[];
    String content;
    
    try {
       content = IOUtils.toString[in, StandardCharsets.UTF_8];
     } finally {
       IOUtils.closeQuietly[in];
     }
    
    9

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    0
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    1
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    2

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    3
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    0
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    5
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    6
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    7

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    0
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    1
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    2

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    3
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    0
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    5
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    6
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    7

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    9
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    0

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    3
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    2

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8
    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    5
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    2

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    2

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1
    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    8
    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
    
    5
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    0

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    4
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    55____56
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    7

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8+=1 +=2

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    4
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    55____56
    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    7

    while [[val = br.readLine[]] != null]   // condition
     {    
       System.out.println[val];             // execution if condition is true
      }
    8
    InputStream in = new URL["/path/to/mypage.html"].openStream[];
    String content;
    
    try {
       content = IOUtils.toString[in, StandardCharsets.UTF_8];
     } finally {
       IOUtils.closeQuietly[in];
     }
    
    21

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1StringBuilder1

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1StringBuilder7

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8StringBuilder1

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8+=1 +=2

    BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];
    1+=4

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    8StringBuilder1

    String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
    
    3StringBuilder1

    StringBuilder1

    Output:  


    Chúng ta có thể đọc tệp HTML trong Java không?

    Trong Java, chúng ta có thể trích xuất nội dung HTML và có thể phân tích tài liệu HTML.we can extract the HTML content and can parse the HTML Document.

    Làm thế nào đọc tệp HTML cục bộ trong Java?

    Tải một tài liệu từ một tập tin..
    Vấn đề.Bạn có một tệp trên đĩa chứa HTML, mà bạn muốn tải và phân tích, sau đó có thể thao tác hoặc trích xuất dữ liệu từ ..
    Dung dịch.Sử dụng jsoup.parse [file in, string charsetName, chuỗi baseURI]: file input = file new ["/tmp/input.html"];....
    Description..

    Làm thế nào truyền dữ liệu từ HTML sang Java?

    InputStream in = new URL["/path/to/mypage.html"].openStream[];
    String content;
    
    try {
       content = IOUtils.toString[in, StandardCharsets.UTF_8];
     } finally {
       IOUtils.closeQuietly[in];
     }
    
    8 Files.readLines[]3
    Hình ảnh trường:.
    Mã Java trong servlet: chuỗi username = request.getParameter ["tên người dùng"];Chuỗi mật khẩu = request.getParameter ["mật khẩu"];....
    Output:.

    Chúng ta có thể kết nối HTML với Java không?

    Ngày nay, cách tốt nhất để thêm Java vào HTML là thông qua cái gọi là trang Javaserver [JSP].Một trang JSP là một tệp với phần mở rộng.JSP.Nó là một đánh dấu HTML chứa một số thẻ JSP.the best way to add Java to HTML is through the so-called JavaServer Pages [JSPs]. A JSP page is a file with the extension . jsp. It is an HTML markup containing several JSP tags.

    Bài Viết Liên Quan

    Chủ Đề