1
public static void main(String[] args) throws IOException {
           for (int i=21;i<30;i++){
               path="https://www.super.kg/media/audio/"+i;
               Document dc = Jsoup.connect(path).timeout(6000).get();
               Elements body = dc.select("div.clear");

               for (Element item : body) {
                   String    method = item.select("div.media_mtspan.video_desc_text").html();
                   method= method.replaceAll("<br>", "\n");
                   System.out.println(method);

                   PrintWriter writer = new PrintWriter("C:\\Users\\cholp\\Desktop\\out.txt", "UTF-8");
                    writer.println(method);
                    writer.close();
                }

            } 
}

help me please, cant write variable "method" to file. after running the programm there is nothing in file

3
  • System.out.println(method); Can you see 'method' variable in console? Commented Apr 22, 2018 at 9:14
  • yes, it gives in console Commented Apr 22, 2018 at 9:22
  • I wouldn't recommend using selector like div.clear because .clear is very likely a class to style the layout and may select lots of unwanted elements, in your case I would recommend using selector such as div.media_mt, which is meaningful in the particular music website. Commented Apr 22, 2018 at 9:35

1 Answer 1

1

Here is the problem:

    for (Element item : body) {
        String method = item.select("div.media_mtspan.video_desc_text").html();
        method = method.replaceAll("<br>", "\n");
        System.out.println(method);

        // Here
        PrintWriter writer = new PrintWriter("C:\\Users\\cholp\\Desktop\\out.txt", "UTF-8");
        writer.println(method);
        writer.close();
    }

PrintWriter(String fileName) will use the following code to construct a PrintWriter instance:

public PrintWriter(String fileName) throws FileNotFoundException {
    this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
         false);
}

By default, FileOutputStream truncates the file and print the content (method), as a result the last method will be print in the file, and, the last method happens to be empty, so the writing seems not work but actually it does, to solve the problem, try to replace new PrintWriter("C:\\Users\\cholp\\Desktop\\out.txt", "UTF-8") with new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\cholp\\Desktop\\out.txt"), "UTF-8")), /*append*/ true), or better, pull the writer out of for loop:

    PrintWriter writer = new PrintWriter("C:\\Users\\cholp\\Desktop\\out.txt", "UTF-8");

    for (Element item : body) {
        String method = item.select("div.media_mtspan.video_desc_text").html();
        method = method.replaceAll("<br>", "\n");
        System.out.println(method);

        writer.append(method);
    }

    writer.close();
Sign up to request clarification or add additional context in comments.

3 Comments

It's the ctor that truncates, not the print* methods. Use new PrintWriter (new OutputStreamWriter (new FileOutputStream (filename, true/*append*/), charset)) -- or new PW (new FOS (,true)) if the default charset is acceptable. Or usually better open it once, before the loop, and close after.
@dave_thompson_085 Thanks for reminding, I'll update my answer for future reader
@dave_thompson_085 , KID94 thank you for your explanation, i get what i wanted)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.