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[]
và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]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]; }
2InputStream in = new URL["/path/to/mypage.html"].openStream[]; String content; try { content = IOUtils.toString[in, StandardCharsets.UTF_8]; } finally { IOUtils.closeQuietly[in]; }
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ải thiện bài viết
Lưu bài viết
Đọc
Approaches:
- Sử dụng Filereader
- 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:
- Sử dụng trình filereader để đọc tệp từ thư mục nguồn và xa hơn nữa
- Nối mỗi dòng vào trình xây dựng chuỗi.
- 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[].
- 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];
}
9String 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[];
2String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
3String 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[];
7String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8String 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[];
0String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
3String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
2String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
4String 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[];
6String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
8String 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[];
8BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];5
String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
2BufferedReader 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[]]];1
StringBuilder
1BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];1
StringBuilder
3BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];1
StringBuilder
5BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];1
StringBuilder
7String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8StringBuilder
1String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8+=
1 +=
2BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];1
+=
4String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8StringBuilder
1String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
3StringBuilder
1StringBuilder
1
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];
}
9String 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[];
2String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
3String 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[];
7String 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[];
2String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
3String 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[];
7String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8String 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[];
0String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
3String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
2String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];5
String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].outerHtml[];
2BufferedReader 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 +=
2BufferedReader 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];
}
21BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];1
StringBuilder
1BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];1
StringBuilder
7String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8StringBuilder
1String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8+=
1 +=
2BufferedReader br = new BufferedReader[new InputStreamReader[URL.openStream[]]];1
+=
4String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
8StringBuilder
1String content = Jsoup.parse[new File["/path/to/mypage.html"], "UTF-8"].toString[];
3StringBuilder
1StringBuilder
1
Output:
Chúng ta có thể đọc tệp HTML trong Java không?
Làm thế nào đọc tệp HTML cục bộ trong Java?
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