Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading entire html file to String?

Tags:

java

file-io

Are there better ways to read an entire html file to a single string variable than:

    String content = "";     try {         BufferedReader in = new BufferedReader(new FileReader("mypage.html"));         String str;         while ((str = in.readLine()) != null) {             content +=str;         }         in.close();     } catch (IOException e) {     } 
like image 664
membersound Avatar asked Aug 20 '12 09:08

membersound


People also ask

How do you read the contents of a file into a string in Java?

The readString() method of File Class in Java is used to read contents to the specified file. Return Value: This method returns the content of the file in String format. Note: File. readString() method was introduced in Java 11 and this method is used to read a file's content into String.

How do I convert HTML text to normal text in Java?

Just call the method html2text with passing the html text and it will return plain text.

How do you process HTML in Java?

Its party trick is a CSS selector syntax to find elements, e.g.: String html = "<html><head><title>First parse</title></head>" + "<body><p>Parsed HTML into a doc. </p></body></html>"; Document doc = Jsoup. parse(html); Elements links = doc.


2 Answers

There's the IOUtils.toString(..) utility from Apache Commons.

If you're using Guava there's also Files.readLines(..) and Files.toString(..).

like image 135
Johan Sjöberg Avatar answered Sep 20 '22 08:09

Johan Sjöberg


You should use a StringBuilder:

StringBuilder contentBuilder = new StringBuilder(); try {     BufferedReader in = new BufferedReader(new FileReader("mypage.html"));     String str;     while ((str = in.readLine()) != null) {         contentBuilder.append(str);     }     in.close(); } catch (IOException e) { } String content = contentBuilder.toString(); 
like image 32
Jean Logeart Avatar answered Sep 19 '22 08:09

Jean Logeart