import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HtmlMerger {
public static void mergeHtmls(List<String> htmlFiles, String outputHtmlFile) throws IOException {
// Create a map to store unique HTML snippets, indexed by a simple hash.
Map<String, String> htmlCache = new HashMap<>();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputHtmlFile))) {
for (String htmlFile : htmlFiles) {
try (BufferedReader reader = new BufferedReader(new FileReader(htmlFile))) {
String htmlContent = reader.readAllText();
// Generate a unique hash for the HTML content. Simple hash for demo.
String htmlHash = generateHash(htmlContent);
// Check if the HTML content is already in the cache.
if (htmlCache.containsKey(htmlHash)) {
// Reuse the cached HTML content.
writer.write(htmlCache.get(htmlHash));
} else {
// Add the HTML content to the cache.
htmlCache.put(htmlHash, htmlContent);
writer.write(htmlContent);
}
}
}
}
}
// Simple hash function for HTML content. Can be improved for better distribution.
private static String generateHash(String html) {
return String.valueOf(html.hashCode());
}
public static void main(String[] args) throws IOException {
// Example usage:
List<String> htmlFiles = new ArrayList<>();
htmlFiles.add("file1.html");
htmlFiles.add("file2.html");
htmlFiles.add("file3.html");
mergeHtmls(htmlFiles, "merged.html");
}
}
Add your comment