Text censorer Learn programming Java

Lesson:

File Management


Exercise:

Text censorer 14


Objetive:

Create a program to "censor" text files. It must read a text file, and dump its results to a new text file, replacing certain words with "[CENSORED]". The words to censor will be in stored a second data file, a text file which will contain a word in each line.


Code:

import java.util.*;
public class Main
{
	private static String[] CreateDictionary(String fileName)
	{
		String[] result;
		java.io.FileReader inputFile;
		java.io.BufferedReader inputFileBufferedReader = new java.io.BufferedReader(inputFile);
		String line;
		int numLines = 0;

		if ((new java.io.File(fileName)).isFile())
		{
			try
			{
				// First: count lines
				inputFile = new java.io.FileReader(fileName);
				do
				{
					line = inputFileBufferedReader.readLine();
					if (line != null)
					{
						numLines++;
					}
				} while (line != null);
				inputFile.close();
				result = new String[numLines];

				// Then: store lines
				inputFile = new java.io.FileReader(fileName);
				int currentLineNumber = 0;
				do
				{
					line = inputFileBufferedReader.readLine();
					if (line != null)
					{
						result[currentLineNumber] = line;
						currentLineNumber++;
					}
				} while (line != null);
				inputFile.close();
				return result;
			}
			catch (RuntimeException e)
			{
				return null;
			}
		}
		return null;
	}

	public static void main(String[] args)
	{
		java.io.InputStreamReader inputFile;
		java.io.OutputStreamWriter outputFile;
		String line;
		String name;

		if (args.length < 1)
		{
			System.out.println("Not enough parameters!");
			System.out.print("Enter file name: ");
			name = new Scanner(System.in).nextLine();
		}
		else
		{
			name = args[0];
		}

		if (!(new java.io.File(name)).isFile())
		{
			System.out.println("File not found!");
			return;
		}

		try
		{
			String[] wordsList = CreateDictionary("dictionary.txt");
			inputFile = File.OpenText(name);
			outputFile = File.CreateText(name + ".censored");

			do
			{
				line = inputFile.ReadLine();
				if (line != null)
				{
					// For each line, we must replace any censored word
					for (String censoredWord : wordsList)
					{
						if (line.contains(" " + censoredWord + " "))
						{
							line = line.replace(" " + censoredWord + " ", " [CENSORED] ");
						}

						if (line.contains(" " + censoredWord + "."))
						{
							line = line.replace(" " + censoredWord + ".", " [CENSORED].");
						}

						if (line.contains(" " + censoredWord + ","))
						{
							line = line.replace(" " + censoredWord + ",", " [CENSORED],");
						}

						if (line.endsWith(" " + censoredWord))
						{
							line = line.substring(0, line.lastIndexOf(" " + censoredWord));
							line += " [CENSORED]";
						}

					}
					// Finally, changes are saved
					outputFile.write(line + System.lineSeparator());
				}

			} while (line != null);

			inputFile.close();
			outputFile.close();
		}
		catch (PathTooLongException e)
		{
			System.out.println("Entered path was too long");
			return;
		}
		catch (IOException ex)
		{
			System.out.printf("Input/output error: %1$s" + "\r\n", ex.getMessage());
			return;
		}
		catch (RuntimeException ex)
		{
			System.out.printf("Unexpected error: %1$s" + "\r\n", ex.getMessage());
			return;
		}

	}
}