Question

In: Computer Science

This assignment covers file I/O. Must design and code a program that can accomplish four things:...

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.

Solutions

Expert Solution

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.


Related Solutions

Must be written in JAVA Code Modify the program you created in previous project to accomplish...
Must be written in JAVA Code Modify the program you created in previous project to accomplish the following: Support the storing of additional user information: street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' Store the data in an ArrayList object Program to Modify import java.util.ArrayList; import java.util.Scanner; public class Address { String firstname; String lastname; int zipcode;    Address(String firstname,String...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates...
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O:...
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O: B) Discuss Serialization, what is it? What are the processes and features? What is the function of the keyword Transient?
How can I fix this code to accomplish the goal of reading and writing on binary...
How can I fix this code to accomplish the goal of reading and writing on binary or text files? 3 import java.io.*; 4 import java.io.FileOutputStream; 5 import java.io.FileInputStream; 6 import java.util.Scanner; 7 8 public class ReadAndWrite implements Serializable 9 { 10 public static void main(String[] args) 11 { 12 boolean file = true; 13 Scanner inputStream; 14 PrintWriter outputStream; 15 FileInputStream inputBinary; 16 FileOutputStream readBinary; 17 FileInputStreamText writeText; 18 FIleOutputStreamText readText; 19 StringBuffer contents = new StringBuffer(); 20 Scanner keyboard...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
If an employee is placed in a Hearing Conservation program, what four things must the employer...
If an employee is placed in a Hearing Conservation program, what four things must the employer provide of perform?
What is the purpose of the XDC file? Simulate the behavioral code Map the I/O from...
What is the purpose of the XDC file? Simulate the behavioral code Map the I/O from VHDL to Basys3 Board Edit the Basys3 Board device type Run the synthesis What is the purpose of declaring a “signal”? Which file contains the list of input combinations used to simulate a circuit in Xilinx Vivado?   XDC Design Testbench What is the purpose of using ieee.std_logic_unsigned.alL or ieee.std_logic_signed.all in lab4 and lab6 testbench2?
//Write in C++ //use this evote code and add File I/O operations (as specified in the...
//Write in C++ //use this evote code and add File I/O operations (as specified in the comments) to it. #include<iostream> using namespace std; int main() {int choice; int bezos = 0 , gates = 0 , bugs = 0 ; int vc = 0 ; do {    cout<<"\n\n\nEVOTE\n-----"    <<"\n1.Jeff Bezos    <<"\n2.Bill Gates    <<"\n3.Bugs Bunny" // 4. Print current tally [hidden admin option] // 5. Print audit trail [hidden admin option] // 6. mess with the vote...
I have to code the assignment below. I cannot get the program to work and I...
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help! Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than...
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O,...
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED a) open an existing text file passed to your program as a command-line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT