150 html files are in a folder on a windows machine.Among them,content of some files (not the file names)contain the sentence "Hello World??".
How to count no of such files using java program

Dani AI

Generated

As asked by : the straightforward way is to scan each HTML file’s text and test for the literal phrase "Hello World??". was right to point to reading files; below are compact, safe Java patterns and a few gotchas (and yes, ’s counting joke is noted).

Simple, memory-frugal line scan (won't match if the phrase is split across lines or interrupted by tags):

import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.io.BufferedReader;

long count = Files.list(dir)
    .filter(p -> p.toString().toLowerCase().endsWith(".html"))
    .filter(p -> {
        try (BufferedReader r = Files.newBufferedReader(p, StandardCharsets.UTF_8)) {
            String line;
            while ((line = r.readLine()) != null) {
                if (line.contains("Hello World??")) return true;
            }
        } catch (Exception e) { /* log or ignore */ }
        return false;
    })
    .count();

If the sentence may be split by HTML tags (for example Hello <b>World</b>??) parse the HTML to text first (using a small HTML parser such as jsoup) and then search the extracted text:

// pseudocode: requires jsoup on the classpath
String text = Jsoup.parse(htmlFile, "UTF-8").text();
if (text.contains("Hello World??")) { /* matched */ }

Quick tips:

  • Use Pattern.quote(target) if switching to regex so question marks are treated literally.
  • Pick StandardCharsets.UTF_8 or the real encoding of your files.
  • For recursive folders use Files.walk(dir); for many files use parallel streams carefully.
  • Log failures (IO/encoding) and skip binary files.

These patterns cover most real-world cases: plain-text match, HTML-tag-splitting, encodings, and performance tradeoffs.

Recommended Answers

All 2 Replies

Read each of them and use Regular Expressions to parse them.

1, 2, 3, 4, ..., 148, 149, 150

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.