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

write a java program a class named textFile. an object of type textFile counts c

ID: 674720 • Letter: W

Question

write a java program a class named textFile. an object of type textFile counts characters, and lines in a text file. The text file is assumed to encode each character in a single byte, using the normal ASCII/UTF-8 standard.

You will need to use FileInputStream for read the text file

Your textFile class must implement exactly the public constructors(textFile) and methods (getCharCount,getLineCount,toString) to implement.

Carefully note this sentence in the constructor's description: All file I/O is complete once this constructor returns.

Your class definition will begin something like this…

public class TextFileAnalyzer {

// You'll probably want some private fields

/** * Constructs a new TextFileAnalyzer that reads from the given file. All file

* I/O is complete once this constructor returns.

* @param filePath a path to the file

* @throws Exception if any errors are generated by the Java API while reading

* from the file */

public TextFileAnalyzer(String filePath) throws Exception {

// This is your job... }

/**

* Returns the number of single-byte characters in the file, including whitespace.

* * @return the number of characters in the file */

public int getCharCount() { // This is your job too... } // and more ...

Explanation / Answer

public static int countLines(String filename) throws IOException { InputStream is = new BufferedInputStream(new FileInputStream(filename)); try { byte[] c = new byte[1024]; int count = 0; int readChars = 0; boolean empty = true; while ((readChars = is.read(c)) != -1) { empty = false; for (int i = 0; i