Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This is a command-based program where what you do is enter what string you want to remove from a text file, than which text file you want to modify and return its result.
There is some documentation at the top of the code so that way you guys can understand how this works. The modified text file is always saved as ResultFile.txt.
The way on how to run it perfectly is to go onto the Microsoft Command Prompt, go to the directory where the class file is held, making sure all of the JAVA resources are there, then type in the following:
java Chapter14Exercise11 abc HelloThere.txt
"abc" is the string to be removed from the text file "HelloThere.txt."
/*
* Data Structures and Object-Oriented Programming - Assignment 6
* Question 14.11 of Introduction to Java Programming (by Daniel Liang)
*
* Text Remover (Command Prompt fashion)
*
* This program opens up an existing file and removes a string, both
* specified through the console that originally calls and executes
* the program. The resulting file is ResultFile.txt; if the Scanner
* object and the PrintWriter object refer to the same File object,
* the result will be a full blank file with zero bytes stored in memory.
*
* Programmed by Gregory Desrosiers
*
* Start Date: March 10, 2013
* End Date: March 10, 2013
*
* File Name: Chapter14Exercise11.java
* Teacher: Amin Ranj Bar
*
* *NOTE*: All strings to be removed, including single characters, are
* case-sensitive.
*
* Small Change made on March 13, 2013: The "throws IOException" clause after the
* brackets in the header of the main has been removed because IOException is a
* subclass of Exception and is being handled by the try-catch-finally block.
*
*/
import java.util.Scanner;
import java.io.*;
public class Chapter14Exercise11
{
public static void main(String[] args)
{
boolean programComplete = false;
Scanner fileReader = null;
PrintWriter fileWriter = null;
if(args.length == 0)
{
System.out.print("You have not specified what string to remove in the file and what file to access.\n\n" +
"Please execute program with the desired associate commands.");
quit();
}
else if (args.length == 1)
{
System.out.print("What file do you want the program to access and remove the string?\nYou have not specified the file.\n\nPlease enter file name.");
quit();
}
else if (args.length == 2 && !args[1].contains("."))
{
System.out.print("The file you specified does not contain a period. You need to enter the file extension.\n\n");
quit();
}
try
{
fileReader = new Scanner(new File(args[1]));
fileWriter = new PrintWriter(new File("ResultFile.txt"));
while(fileReader.hasNext())
{
String currentLine = fileReader.nextLine();
fileWriter.println(currentLine.replaceAll(args[0], ""));
}
System.out.print("File Operation complete. ");
programComplete = true;
}
catch (Exception error)
{
if (error instanceof FileNotFoundException)
System.err.println("Sorry! The file has not been found.");
else if (error instanceof IOException)
System.err.println("Something went wrong with either writing or reading the " +
"specified files. Needs to close.");
// Debugging purpose only
else if (error instanceof ClassNotFoundException)
{
System.err.println("Required classes have not been found.");
error.printStackTrace();
}
}
finally
{
if (fileReader != null)
fileReader.close();
if (fileWriter != null)
fileWriter.close();
if (programComplete)
System.out.println("Program complete.");
}
}
public static void quit()
{
System.out.println("Program terminated.");
System.exit(1);
}
}
That does it for this code! Have a nice night!
* Data Structures and Object-Oriented Programming - Assignment 6
* Question 14.11 of Introduction to Java Programming (by Daniel Liang)
*
* Text Remover (Command Prompt fashion)
*
* This program opens up an existing file and removes a string, both
* specified through the console that originally calls and executes
* the program. The resulting file is ResultFile.txt; if the Scanner
* object and the PrintWriter object refer to the same File object,
* the result will be a full blank file with zero bytes stored in memory.
*
* Programmed by Gregory Desrosiers
*
* Start Date: March 10, 2013
* End Date: March 10, 2013
*
* File Name: Chapter14Exercise11.java
* Teacher: Amin Ranj Bar
*
* *NOTE*: All strings to be removed, including single characters, are
* case-sensitive.
*
* Small Change made on March 13, 2013: The "throws IOException" clause after the
* brackets in the header of the main has been removed because IOException is a
* subclass of Exception and is being handled by the try-catch-finally block.
*
*/
import java.util.Scanner;
import java.io.*;
public class Chapter14Exercise11
{
public static void main(String[] args)
{
boolean programComplete = false;
Scanner fileReader = null;
PrintWriter fileWriter = null;
if(args.length == 0)
{
System.out.print("You have not specified what string to remove in the file and what file to access.\n\n" +
"Please execute program with the desired associate commands.");
quit();
}
else if (args.length == 1)
{
System.out.print("What file do you want the program to access and remove the string?\nYou have not specified the file.\n\nPlease enter file name.");
quit();
}
else if (args.length == 2 && !args[1].contains("."))
{
System.out.print("The file you specified does not contain a period. You need to enter the file extension.\n\n");
quit();
}
try
{
fileReader = new Scanner(new File(args[1]));
fileWriter = new PrintWriter(new File("ResultFile.txt"));
while(fileReader.hasNext())
{
String currentLine = fileReader.nextLine();
fileWriter.println(currentLine.replaceAll(args[0], ""));
}
System.out.print("File Operation complete. ");
programComplete = true;
}
catch (Exception error)
{
if (error instanceof FileNotFoundException)
System.err.println("Sorry! The file has not been found.");
else if (error instanceof IOException)
System.err.println("Something went wrong with either writing or reading the " +
"specified files. Needs to close.");
// Debugging purpose only
else if (error instanceof ClassNotFoundException)
{
System.err.println("Required classes have not been found.");
error.printStackTrace();
}
}
finally
{
if (fileReader != null)
fileReader.close();
if (fileWriter != null)
fileWriter.close();
if (programComplete)
System.out.println("Program complete.");
}
}
public static void quit()
{
System.out.println("Program terminated.");
System.exit(1);
}
}
That does it for this code! Have a nice night!
0 comments:
Post a Comment