1. import java.io.*;
  2. import java.util.*;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. public class HtmlGrouper {
  6. private static final String HTML_FILE_PATH = "input.html"; // Replace with your HTML file
  7. private static final String OUTPUT_FILE_PATH = "output.txt"; // Replace with your desired output file
  8. private static final String GROUP_START_TAG = "<div class=\"group\">"; // Tag to identify groups
  9. private static final String GROUP_END_TAG = "</div>"; // Tag to identify end of groups
  10. public static void main(String[] args) {
  11. try {
  12. groupHtmlEntries(HTML_FILE_PATH, OUTPUT_FILE_PATH);
  13. System.out.println("Grouping complete. Output written to " + OUTPUT_FILE_PATH);
  14. } catch (IOException e) {
  15. System.err.println("Error processing HTML: " + e.getMessage());
  16. e.printStackTrace();
  17. }
  18. }
  19. public static void groupHtmlEntries(String inputFilePath, String outputFilePath) throws IOException {
  20. try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
  21. BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
  22. String line;
  23. boolean inGroup = false;
  24. while ((line = reader.readLine()) != null) {
  25. if (line.contains(GROUP_START_TAG)) {
  26. inGroup = true;
  27. writer.write(line); // Write the start tag
  28. } else if (line.contains(GROUP_END_TAG)) {
  29. inGroup = false;
  30. writer.write(line); // Write the end tag
  31. } else if (inGroup) {
  32. writer.write(line); // Write lines within the group
  33. }
  34. }
  35. }
  36. }
  37. }

Add your comment