Hướng dẫn string to html file java - chuỗi vào tệp html java

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.

...
      System.out.println("Final Register");
      for (int i=0; i < ch.length; i++)
        {
         System.out.println(cd.getDenomLiteralAt(i)+" "+cd.getDenomNumAt(i));
        }
   }

...

Hỏi ngày 25 tháng 7 năm 2012 lúc 5:35Jul 25, 2012 at 5:35

Hướng dẫn string to html file java - chuỗi vào tệp html java

Kerry Gkerry gKerry G

9014 Huy hiệu vàng12 Huy hiệu bạc21 Huy hiệu đồng4 gold badges12 silver badges21 bronze badges

3

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.

Xem bản demo dưới đây

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
}

Đã trả lời ngày 25 tháng 7 năm 2012 lúc 6:19Jul 25, 2012 at 6:19

Hướng dẫn string to html file java - chuỗi vào tệp html java

MavroscymavroscyMaVRoSCy

17.5K14 Huy hiệu vàng79 Huy hiệu bạc122 Huy hiệu đồng14 gold badges79 silver badges122 bronze badges

3

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.

Đã trả lời ngày 25 tháng 7 năm 2012 lúc 5:38Jul 25, 2012 at 5:38

DandanDan

3.1561 Huy hiệu vàng 30 Huy hiệu bạc49 Huy hiệu đồng1 gold badge30 silver badges49 bronze badges

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

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

    Lưu bài viết

    Đọc

    Approaches:

    1. Bàn luận
    2. 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:

    1. Sử dụng url.openstream ()
    2. 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:
    3. Sử dụng trình filereader để đọc tệp từ thư mục nguồn và xa hơn nữabr.close().
    4. 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
    }
    
    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
    }
    
    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
    }
    
    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
    }
    
    6

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7
    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    8
    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    9

    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
    }
    
    4
    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

    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
    }
    
    4
    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    4
    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    1
    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    6

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7
    while ((val = br.readLine()) != null)   // condition
     {    
       System.out.println(val);             // execution if condition is true
      }
    8

    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
    }
    
    4
    while ((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
    }
    
    8

    import4import5

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7import7

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7import9

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7java.io.*;1

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7java.io.*;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
    }
    
    4import7

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

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7public0

    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
    }
    
    4import7

        import7

    import7

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7import0 import1import2import3 

    Hướng dẫn string to html file java - chuỗi vào tệp html java

    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
    }
    
    8

    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
    }
    
    4
    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()));
    7
    while ((val = br.readLine()) != null)   // condition
     {    
       System.out.println(val);             // execution if condition is true
      }
    8

    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
    }
    
    4
    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    4
    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    1
    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    6

    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
    }
    
    4
    while ((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
    }
    
    8

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

    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
    }
    
    4
    while ((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
    }
    
    8

    import4

    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
    }
    
    07

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7import7

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7java.io.*;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
    }
    
    4import7

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

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7public0

    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
    }
    
    4import7

        import7

    import7

    BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
    7import0 import1import2import3 

    Hướng dẫn string to html file java - chuỗi vào tệp html java


    Chúng ta có thể viết mã HTML trong Java không?

    Làm thế nào để tạo một trình soạn thảo HTML trong Java..
    Nhập một số gói swing và AWT như sau: ....
    Bây giờ tạo một lớp mở rộng JPanel và thực hiện ActionListener. ....
    Bây giờ tạo một hàm tạo để khởi tạo các thành phần của Swing và HTML. ....
    Bây giờ hãy tạo một sự kiện ActionPerformed hoạt động trên nút của chúng tôi "bấm để thay đổi" ..

    Làm thế nào để làm phân tích cú pháp HTML trong Java?

    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.

    Làm thế nào để đọc chuỗi HTML trong Java?

    Thực hiện theo các bước dưới đây:..
    Xác định HTML là một chuỗi ..
    Sử dụng phương thức Parse (Chuỗi HTML) của lớp JSOUP trả về đối tượng tài liệu sau khi xử lý chuỗi HTML ..
    Sử dụng phương thức tiêu đề () của lớp tài liệu để có được tiêu đề ..
    In tiêu đề ..

    J2HTML là gì?

    Đầ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.