Texto censurado Curso de Programación Java

Lección:

Administración de Archivos


Ejercicio:

Texto censurado 8


Objetivo:

Cree un programa para "censurar" archivos de texto. Debe leer un archivo de texto y volcar sus resultados a un nuevo archivo de texto, reemplazando ciertas palabras con "[CENSURADO]". Las palabras a censurar se almacenarán en un segundo archivo de datos, un archivo de texto que contendrá una palabra en cada línea.


Código:

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;
		}

	}
}