将文档写入文件jsoup

时间:2018-01-16 06:11:40

标签: java file jsoup document

我正在尝试将Document的body标签保存到文件中。此代码只保存一个空白文件。请帮忙

 Document d = Jsoup.connect("https://tin.tin.nsdl.com/oltas/servlet/TanSearch/?appUser=T&TAN_NO=JBPP04182D&TAN_FROM_DT_DD=01&TAN_FROM_DT_MM=10&TAN_FROM_DT_YY=2017&TAN_TO_DT_DD=12&TAN_TO_DT_MM=01&TAN_TO_DT_YY=2018&HID_IMG_TXT=" + captcha + "&submit=Download%20Challan%20file").cookies(cookies).get();

       // System.out.println(d);

        BufferedWriter  writer = null;
        try
        {
            writer = new BufferedWriter( new FileWriter("F://test.csi"));
            writer.write(d.select("body").text());

        }
        catch ( IOException e)
        {
        }

1 个答案:

答案 0 :(得分:1)

我认为问题在于你没有关闭作家

package so;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; 

public class JsoupEx {
public static void main(String[] args)  {

        BufferedWriter  writer = null;
        try
        {
            Document d = Jsoup.connect("https://www.google.com").get();
            writer = new BufferedWriter( new FileWriter("/home/Desktop/test.txt"));
            writer.write(d.select("body").text());
            writer.close();  // add this line
        }
        catch ( IOException e)
        {
            System.out.println(e);
        }
    }
}

不关闭编写器会将更新保留在缓冲区中,不会被推送到文档中,即缓冲区不会被刷新。