Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(in java programming language) Lab 10: Files and Streams (2) Character Streams_

ID: 3729601 • Letter: #

Question

(in java programming language)

Lab 10: Files and Streams (2) Character Streams_ Sequential-Access Text Files (2) Create a class MyFileReader with the following: One private instance variable of type Scanners. b. a. Another private instance variable of type File c. A constructor for MyFileReader class to initialize File with a given file name. If the file doesn't exist or if it is not readable, throw IllegalArgumentException d. Two methods openFile and closeFile to open and close the scanner. e. A Boolean method that searches for a given word. f. An in 8. A method that prints the number of words and the number of characters in a given line number. h. A method that copies the content of the current file to another and converts any string "java" to t method that returns number of occurrences for a given word that is not case sensitive. upper case. Create a test class to demonstrate MyFileReader capabilities. Handle all exceptions by using try-catch.

Explanation / Answer

import java.util.Scanner;

import java.io.*;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

class MyFileReader

{

MyFileReader(String file)

{

Scanner scan = new Scanner(System.in);

scan.useDelimiter(" ");

System.out.println("Enter DONE to terminate");

try

{

String line = null;

while ((line = scan.nextLine()) != null)

{

if (line.trim().equalsIgnoreCase("done"))

{

System.out.println("Exiing");

System.exit(0);

}

try{   

FileWriter fw=new FileWriter(file);   

fw.write(line);   

fw.close();   

}

catch(Exception e){System.out.println(e); }   

}

}

catch(Exception ex)

{

System.out.println(ex);   

}

}

public static void main(String args[])

{

String file = "ReadContent.txt";

String WFile = "WriteContent.txt";

MyFileReader mfr = new MyFileReader(file);

try

{

File sourceFile = new File("ReadContent.txt");

File destFile = new File("WriteContent.txt");

if (!destFile.exists())

{

try

{

destFile.createNewFile();

}

catch (IOException e)

{

e.printStackTrace();

}

}

InputStream input = null;

OutputStream output = null;

/* FileInputStream to read streams */

input = new FileInputStream(sourceFile);

/* FileOutputStream to write streams */

output = new FileOutputStream(destFile);

byte[] buf = new byte[1024];

int bytesRead;

while ((bytesRead = input.read(buf)) > 0)

{

output.write(buf, 0, bytesRead);

}

}

catch(Exception e) { }

  

}

}