In: Computer Science
This assignment covers file I/O. Must design and code a program that can accomplish four things: copy one file to another (i.e., place the contents of file A into file B), ‘compress’ a file by removing all vowels (a, e, i, o, u, and y) from the file, merge two files, and finally double each line of an input file, saving into an output file. Also make it so the input file's content can be changed from the example given. Please send a test run as well. Please and thank you for your help.
Program Specifications
The program should be menu driven with the top level menu being:
----------------------------------------------------------- Welcome to the File Copier
1) copy file A to file B
2) ‘compress’ file A (remove all vowels from the file), saving into file B
3) ‘double’ file A -- for each character in the input file, repeat it twice in the output file B.
4) ‘merge’ file A and file B saving into file C. (places all of file A in output file C, then adds all of file B after that into file C).
5) quit the program
-----------------------------------------------------------
Appropriate user prompts shall be provided for each choice (e.g.; for selection 2, you must prompt the user for the name of files A and B). Be sure to keep asking them what they want to do until they ask to quit the program. Be sure to check for errors.
For reversing, and compressing a file here is an example:
Input file:
This is a test. The lazy dog ran over the moon.
Able was I ere I saw elba.
I don't know what to type.
Output File (doubled):
TThhiiss iiss aa tteesstt.. TThhee llaazzyy ddoogg rraann oovveerr tthhee mmoooonn..
AAbbllee wwaass II eerree II ssaaww eellbbaa..
II ddoonn''tt kknnooww wwhhaatt ttoo ttyyppee..
Output File (compressed):
Ths s tst. Th lz dg rn vr th mn. bl ws r sw lb. dn't knw wht t tp.
Please follow the code and comments for description :
CODE :
// required imports
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FileOperations // class to run the code
{
public static void main(String[] args) throws
FileNotFoundException, IOException // driver method
{
Scanner sc = new
Scanner(System.in);
// local
instances
FileInputStream instream
= null;
FileOutputStream
outstream = null;
BufferedReader br =
null;
BufferedReader r =
null;
BufferedWriter bw =
null;
while (true) //
iterate till the end
{
System.out.println("Welcome to the File Copier"); // message as
prompt
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println(" 1) Copy file A to file B.\n"
+ " 2) 'compress' file A (remove all vowels from the file), saving
into file B.\n"
+ " 3) 'double' file A -- for each character in the input file,
repeat it twice in the output file B.\n"
+ " 4) 'merge' file A and file B saving into file C. (places all of
file A in output file C, then adds all of file B after that into
file C).\n"
+ " 5) Quit the Program.");
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------");
System.out.print("Your Choice : "); // message
int choice = sc.nextInt(); // read the data
switch (choice) // switch based on the choice
{
case 1:
System.out.print("Enter the First File Name : ");
sc.nextLine();
String f1 = sc.nextLine();
File infile = new File(f1);
System.out.print("Enter the Second File Name : ");
String f2 = sc.nextLine();
File outfile = new File(f2);
try
{
instream = new FileInputStream(infile);
outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
int length;
System.out.println("Copying the Data....");
// copying the contents from input stream to output stream using
read and write methods
while ((length = instream.read(buffer)) > 0)
{
outstream.write(buffer, 0, length);
}
//Closing the input/output file streams
instream.close();
outstream.close();
System.out.println("File Copied Successfully!!");
} catch (IOException ioe)
{
ioe.printStackTrace();
}
break;
case 2: // case to compress and copy the code
System.out.print("Enter the First File Name : ");
sc.nextLine();
String file1 = sc.nextLine();
File s1 = new File(file1);
System.out.print("Enter the Second File Name : ");
String file2 = sc.nextLine();
File s2 = new File(file2);
System.out.println("Compressed and Copying the data...");
try
{
br = new BufferedReader(new FileReader(s1));
bw = new BufferedWriter(new FileWriter(s2));
String line;
while ((line = br.readLine()) != null)
{
line = line.replaceAll("[AEIOUaeiou]", ""); // replace the
vowels
bw.write(line);
}
br.close();
bw.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
System.out.println("Compressed and Copied Successfully.!!"); //
message
break;
case 3: /// case to double the characters
System.out.print("Enter the First File Name : ");
sc.nextLine();
String fileName1 = sc.nextLine();
File inputfile1 = new File(fileName1);
System.out.print("Enter the Second File Name : ");
String fileName2 = sc.nextLine();
File inputfile2 = new File(fileName2);
System.out.println("Doubling the Characters....");
try
{
br = new BufferedReader(new FileReader(inputfile1));
bw = new BufferedWriter(new FileWriter(inputfile2));
String line;
while ((line = br.readLine()) != null)
{
line = line.replaceAll(".", "$0$0"); // double the characters
bw.write(line);
}
br.close();
bw.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
System.out.println("Doubled and Copied Successfully.!!"); //
message
break;
case 4: // case to merge the files
try
{
ArrayList<String> list = new ArrayList<>();
System.out.print("Enter the First File Name : ");
sc.nextLine();
String inp1 = sc.nextLine();
File inf1 = new File(inp1);
System.out.print("Enter the Second File Name : ");
String inp2 = sc.nextLine();
File inf2 = new File(inp2);
System.out.print("Enter the Third File Name : ");
String inp3 = sc.nextLine();
File outf = new File(inp3);
System.out.println("Merging the two files...");
String string1 = null;
String string2 = null;
br = new BufferedReader(new FileReader(inf1));
r = new BufferedReader(new FileReader(inf2));
while ((string1 = br.readLine()) != null)
{
list.add(string1);
}
while ((string2 = r.readLine()) != null)
{
list.add(string2);
}
bw = new BufferedWriter(new FileWriter(outf));
String listWord;
for (int i = 0; i < list.size(); i++)
{
listWord = list.get(i);
bw.write(listWord);
bw.write("\n");
}
System.out.println("Merging the files Completed...!"); //
message
bw.close();
} catch (IOException e)
{
e.printStackTrace();
}
break;
case 5:
System.out.println("Exiting the code..."); // message
System.exit(0);
break;
default:
System.out.println("Invalid Selection..!"); // message
System.out.println("Please Try Again..");
break;
}
}
}
}
OUTPUT :
Hope this is helpful.