java 在java中读取html文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13289484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:09:22  来源:igfitidea点击:

read the html file in java

javabufferedreaderfile-read

提问by adesh

In java i have to read multiple files to search some text . Files are containing large html data so its difficult to read the content of the html file with the help of following code . Is any direct method to fetch the content of a file using java . I am using the following code but its making my application slow suggest me the best alternate of it

在 java 中,我必须读取多个文件来搜索一些文本。文件包含大量 html 数据,因此很难借助以下代码读取 html 文件的内容。是使用 java 获取文件内容的任何直接方法。我正在使用以下代码,但它使我的应用程序变慢建议我最好的替代方法

try{
   FileReader fr=new FileReader("path of the html file");
   BufferedReader br= new BufferedReader(fr);
    String content="";
   while((s=br.readLine())!=null)
    {

     content=content+s;

    } 

     System.out.println("content is"+content);
   }
  catch(Exception ex)
   {

    }

回答by Peter

String concatenation is always slow when done in a loop

在循环中完成字符串连接总是很慢

You will need to change it to using a StringbBuilder and giving that StringBuilder a decent starting size.

您需要将其更改为使用 StringbBuilder 并为该 StringBuilder 提供合适的起始大小。

FileReader fr=new FileReader("path of the html file");
BufferedReader br= new BufferedReader(fr);
StringBuilder content=new StringBuilder(1024);
while((s=br.readLine())!=null)
    {
    content.append(s);
    }