Advertisement
Java_Volume1 String Manipulation #99636

Html tag stripper

reads in a Html file, removes the tags and sends the output to an output.txt file

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
/*
 *this program reads in a text file,
 *puts a carrage return in at the end of 
 *each sentance. then sends that it to
 *an output text file
 *@author John Hunsley
 *@version 1.0 25/02/03
 *
*/
import java.util.*;
import java.io.*;

class HtmlStripper
{
	public static void main(String args[])
	{
		StringBuffer fileAsStringBuffer = new StringBuffer();//string buffer for file input
		String fileAsString = "intialized";//the file as a string
				
		/*****************read the whole file into a StringBuffer, line by line********************/
		try{
			
			BufferedReader input = new BufferedReader(
									 new FileReader("text.txt"));
			String line;
			while((line = input.readLine()) != null)
			{
				fileAsStringBuffer.append(line);
			}
			input.close();//close the buffer
			
			System.out.println("file saved as string");
			
			fileAsString = fileAsStringBuffer.toString();//set the String in the buffer as a String
		
		}
		catch(IOException e){
			System.out.println("IO Exception occured");
		}
/************strip tags******************/
fileAsString = fileAsString.replaceAll("\\<.*?\\>","");//strips all html tags
//write the String out to a text file
		try{
			
			File file = new File("output.txt");
			PrintWriter output = new PrintWriter(new FileWriter(file));
			output.println(fileAsString);
			output.close();
			System.out.println("String written to output text file");
			JOptionPane.showMessageDialog(null,
							 "String written to output text file");
		}
		catch(IOException e){
			System.out.println("IO Exception occured");
		}
	
}
Original Comments (3)
Recovered from Wayback Machine