Question

Lab Description : Exercise : a) Create an interface named ReadAndWriteFile, that has readAndReturnFile() and writeFile()...

Lab Description :

Exercise :

a) Create an interface named ReadAndWriteFile, that has readAndReturnFile() and writeFile() methods.

b) readAndReturnFile() and writeFile() both throws IOException, FileNotFoundException and Exception.

c) Create a class named DealingWithTextFile that implements ReadAndWriteFile. This class read and write txt files.

d) Create a class named DealingWithCsvFile that implements ReadAndWriteFile.

This class read and write csv files. - Please mention that you have to deal with exceptions, use a dialog message to catch the exceptions.

e) DealingWithTextFile class uses readAndReturnFile() method to read a text file in a. a text file in a specific path "src/R/test.txt" and return ArrayList with the content of the txt file.

. f) DealingWithTextFile class uses writeFile() method to write "test.txt" on a new txt file called "test2.txt".

Your original text file ("test.txt") should be something like:

Name Age Specialized

Ahmad 44 Java

Ali 31 Python

Khaild 90 C++

g) The new text file ("test2.txt") should be :

Name|Age|Specialized

Ahmad|44|Java

Ali|31|Python

Khaild|90|C++

h) DealingWithCsvFile class uses readAndReturnFile() method read csv file a specific path
- Your original text file ("test.txt") should be something like:
"src/R/test.csv".

- The "test.csv" file contains the same data saved in "test.txt", but in csv format.
- Hint: replace empty spaces with commas.

i)DealingWithCsvFile class uses writeFile() method to write "test.csv" file into into a new csv file called "test2.csv".

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Find all file below

ReadAndWriteFile.java

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public interface ReadAndWriteFile {
   ArrayList<String> readAndReturnFile() throws IOException,FileNotFoundException,Exception;
   void writeFile() throws IOException,FileNotFoundException,Exception;

}
--------------------------------------------

DealingWithTextFile.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class DealingWithTextFile implements ReadAndWriteFile{
  
   @Override
   public ArrayList<String> readAndReturnFile(){
       ArrayList<String> content=new ArrayList<String>();
       try {
           File file=new File("src/R/test.txt");
           BufferedReader br=new BufferedReader(new FileReader(file));
           String str=null;
           while((str=br.readLine())!=null) {
               content.add(str);
           }
       }
       catch (FileNotFoundException fileNotFound) {
           System.out.println("File not found");
       }
       catch(IOException ioException) {
           System.out.println("IO exception");
       }
       catch (Exception e) {
           System.out.println("some exception occured");
       }
       return content;
   }

   @Override
   public void writeFile() {
       try {
           ArrayList<String> data=readAndReturnFile();
           File file=new File("src/R/test2.txt");
           file.createNewFile();
           BufferedWriter bw=new BufferedWriter(new FileWriter(file));
           for(int i=0;i<data.size();i++) {
               String[] words=data.get(i).trim().split(" ");
               String line="";
               int j=0;
               for(j=0;j<words.length-1;j++) {
                   line+=words[j];
                   line+="|";
               }
               line+=words[j];
               line+='\n';
               bw.write(line);
           }
           bw.close();
       }
       catch (FileNotFoundException fileNotFound) {
           System.out.println("File not found");
       }
       catch(IOException ioException) {
           System.out.println("IO exception");
       }
       catch (Exception e) {
           System.out.println("some exception occured");
       }
       finally {
          
       }
   }
---------------------------------------------------------------------------

DealingWithCsvFile.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class DealingWithCsvFile implements ReadAndWriteFile{

   @Override
   public ArrayList<String> readAndReturnFile(){
       ArrayList<String> content=new ArrayList<String>();
       try {
           File file=new File("src/R/test.csv");
           BufferedReader br=new BufferedReader(new FileReader(file));
           String str=null;
           while((str=br.readLine())!=null) {
               content.add(str);
           }
       }
       catch (FileNotFoundException fileNotFound) {
           System.out.println("File not found");
       }
       catch(IOException ioException) {
           System.out.println("IO exception");
       }
       catch (Exception e) {
           System.out.println("some exception occured");
       }
       return content;
   }

   @Override
   public void writeFile(){
       try {
           ArrayList<String> data=readAndReturnFile();
           File file=new File("src/R/test2.csv");
           file.createNewFile();
           BufferedWriter bw=new BufferedWriter(new FileWriter(file));
           for(int i=0;i<data.size();i++) {
               bw.write(data.get(i)+"\n");
           }
           bw.close();
       }
       catch (FileNotFoundException fileNotFound) {
           System.out.println("File not found");
       }
       catch(IOException ioException) {
           System.out.println("IO exception");
       }
       catch (Exception e) {
           System.out.println("some exception occured");
       }
      
   }

}

-----------------

Main.java

import java.util.ArrayList;

public class Main {

   public static void main(String[] args) {
       DealingWithTextFile txt=new DealingWithTextFile();
       ArrayList<String> list=txt.readAndReturnFile();
       list.forEach(System.out::println);
       txt.writeFile();
       DealingWithCsvFile csv=new DealingWithCsvFile();
       ArrayList<String> list2=csv.readAndReturnFile();
       list2.forEach(System.out::println);
       csv.writeFile();
   }

}
-------------------------------

place test.csv and test.txt in R folder as shown in below screenshot

Add a comment
Know the answer?
Add Answer to:
Lab Description : Exercise : a) Create an interface named ReadAndWriteFile, that has readAndReturnFile() and writeFile()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter...

    Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter called dataFile (representing the path to a text file) and uses a Set of Strings to eliminate duplicate words from dataFile. The unique words should be stored in an instance variable called uniqueWords. Create an instance method called write that takes a single parameter called outputFile (representing the path to a text file) and writes the words contained in uniqueWords to the file pointed...

  • Write a class named SatData that reads a JSON file containing data on 2010 SAT results...

    Write a class named SatData that reads a JSON file containing data on 2010 SAT results for New York City and writes the data to a text file in CSV (comma-separated values) format. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named save_as_csv that takes as a parameter a list of DBNs (district...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • You will be writing some methods that will give you some practice working with Lists. Create...

    You will be writing some methods that will give you some practice working with Lists. Create a new project and create a class named List Practice in the project. Then paste in the following code: A program that prompts the user for the file names of two different lists, reads Strings from two files into Lists, and prints the contents of those lists to the console. * @author YOUR NAME HERE • @version DATE HERE import java.util.ArrayList; import java.util.List; import...

  • Write a program that reads a file named input.txt and writes a file that contains the...

    Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns...

    Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...

  • Create a text file named “file1.txt” (by use of the notepad editor in Windows for instance)...

    Create a text file named “file1.txt” (by use of the notepad editor in Windows for instance) containing the following integer values, one per line: ​12 5 13 56 90 52 82 52 Write a Java program that reads these values from the file and displays their sum on the screen.

  • write out the appropriate commands for each step using CMD 1, Create a new file named,...

    write out the appropriate commands for each step using CMD 1, Create a new file named, Work1.txt, in the directory HW1a, with the use of the ECHO command with the following sentence 2, Copy the file Work1.txt from directory HW1a to directory HW1aa. 3, Rename the copied file Work1.txt in directory HW1aa to Work2.txt 4, Move the directory HW1aaa to the directory HW1a and rename it HW1ab 5,Move the file Work1.txt from directory HW1a to directory HW1aa 6, Copy both...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT