Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

Text censorer - Practice Exercises Java


Lesson 8:

File management


Exercise 8.38:

Text censorer


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.


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

	}
}
Exercisey 8.38

Share Now:


Google Ads





Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.