Tôi đang cố gắng viết kết quả của phương thức sau vào trang web. Tôi nghĩ rằng có thể làm, tôi chỉ gặp khó khăn trong việc tìm ra chính xác cách làm điều đó. Bất kỳ trợ giúp sẽ được rất đánh giá cao. Cảm ơn.
Trong Java có nhiều cách để ghi dữ liệu trên một tệp. Để viết dữ liệu văn bản, cách dễ nhất là sử dụng một bộ đệm.
Tất cả những gì bạn cần làm là biết đường dẫn đến tệp HTML công khai của máy chủ web của bạn.
Chạy mã Java và ghi vào một tệp bên dưới đường dẫn đó. Nó không khác gì viết vào bất kỳ tệp nào khác trên máy, ngoại trừ cái này mà thế giới có thể thấy. Chỉ có điều là, nếu Java của bạn sẽ tạo tệp, bạn nên chắc chắn đặt các quyền bảo vệ đủ cho tệp. 0755 là an toàn vừa phải.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcBàn luậnXem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Approaches:
- Bàn luận
- HTML là cốt lõi của web, tất cả các trang bạn thấy trên Internet là HTML, cho dù chúng được tạo động bởi JavaScript, JSP, PHP, ASP hoặc bất kỳ công nghệ web nào khác. Trình duyệt của bạn thực sự phân tích HTML và hiển thị nó cho bạn nhưng nếu chúng tôi cần phân tích một tài liệu HTML và tìm một số phần tử, thẻ, thuộc tính hoặc kiểm tra xem một phần tử cụ thể có tồn tại hay 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.
Sử dụng FilereaderThe 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 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:
- Sử dụng trình filereader để đọc tệp từ thư mục nguồn và xa hơn nữabr.close().
- Nối mỗi dòng vào trình xây dựng chuỗi.
Implementation:
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 ().
In ra chuỗi.
Java
import
java.io.*;
public
class
GFG {
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
8
public
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
1 FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
2 FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
3FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
5 FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
6BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
8BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
9FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
0BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
1 BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
2FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
1 BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
6BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
8FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
1 FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
8import
4import
5
BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7import
7BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7import
9BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7java.io.*;
1BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7java.io.*;
3FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4import
7BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
1 while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
6BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7public
0FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4import
7
import
7
import
7
BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7import
0 import
1import
2import
3
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4java.io.*;
7 java.io.*;
8Using the Url.openStream()
- Đầu ra: & nbsp;url.openStream()function that initiates the new TCPconnection to the Server that the URL provides it to.
- Cách tiếp cận 2: Sử dụng 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ó.
BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
- 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 đó.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.
- Thông tin đó ở dạng byte sau đó 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.
inputStreamReader()
- Đầ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.
while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
Implementation:
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 ().
In ra chuỗi.
Java
import
java.io.*;
Java
import
java.io.*;
public
class
GFG {
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
8FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
0BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
1 BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
2BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
8FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
1 BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
6FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
1 FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
8BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
1 while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
6FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
1 FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
8import
4
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
07BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7import
7BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7java.io.*;
3FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4import
7BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
4BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
1 while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
6BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7public
0FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName.html");
writer = new BufferedWriter(fWriter);
writer.write("<span>This iss your html content here</span>");
writer.newLine(); //this is not actually needed for html files - can make your code more readable though
writer.close(); //make sure you close the writer object
} catch (Exception e) {
//catch any exceptions here
}
4import
7
import
7
import
7
BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
7import
0 import
1import
2import
3
Phân tích cú pháp HTML rất đơn giản với JSOUP, tất cả những gì bạn cần gọi là phương thức tĩnh jsoup.parse () và chuyển chuỗi HTML của bạn cho nó.JSOUP cung cấp một số phương thức phân tích quá tải () để đọc tệp HTML từ chuỗi, một tệp, từ một URI cơ sở, từ URL và từ InputStream.all you need to call is static method Jsoup. parse() and pass your HTML String to it. JSoup provides several overloaded parse() methods to read HTML file from String, a File, from a base URI, from an URL, and from an InputStream.
Đầu tiên: J2HTML là người xây dựng Java HTML.Nó không phải là một công cụ mẫu và nó không muốn cạnh tranh với các công cụ mẫu.a Java HTML builder. It's not a template engine, and it doesn't want to compete with template engines.