In Java è possibile rimuovere tutti i tag HTML e salvare una pagina Web come file di testo usando l'espressione regolare \\<.*?>. Questa espressione regolare identifica tutti i tags HTML, cioè stringhe che iniziano con < e terminano con >. Queste stringhe vengono sostituite con la stringa vuota. In questo modo, il file di testo risultante conterrà solo il contenuto visibile nel browser della pagina HTML.
/** * Remove HTML tags from the file content * @param filePath the path of the file */ public void removeHTMLTags(String filePath){ StringBuilder sb = new StringBuilder(); try { //read the file in a string and remove HTML tags BufferedReader br = new BufferedReader(new FileReader(filePath)); String line; while ( (line=br.readLine()) != null) { sb.append(line); } String nohtml = sb.toString().replaceAll("\\<.*?>",""); //delete the input file File input = new File(filePath); input.delete(); //create the new file BufferedWriter out = new BufferedWriter(new FileWriter(filePath)); out.write(nohtml); out.close(); } catch(IOException e) { System.out.println(e.getMessage()); e.printStackTrace(); } }