Question

Using Java programming to answer this question. Write a method countLines(String inFilename) of a class FileUtility...

Using Java programming to answer this question.

Write a method countLines(String inFilename) of a class FileUtility which reads a binary byte file with name inFilename using FileInputStream. The method returns the no. of lines containing comments starting with "//" with optional spaces (but not other characters) before it. Each line is terminated by '\n'. You may write other methods if necessary. Handle possible exception by outputting a suitable message if an exception occurs.

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

CODE:

import java.io.File;
import java.io.FileInputStream;

public class FileContents {
   public static int countLines(String inFilename) {
       int noOfLinesCount = 0;
       File file = new File(inFilename);
       try (FileInputStream fileInputStream = new FileInputStream(file)) {
           int line;
           // Reading character by character form fileInputStream Object
           while ((line = fileInputStream.read()) != -1) {
               // If one character is '/'
               if ((char) line == '/') {
                   // Then we are seaching for next character that it has to be '/' for success
                   // comment
                   if ((char) (line = fileInputStream.read()) == '/') {
                       // Increment the lines count
                       noOfLinesCount++;
                   }
               }
           }
       } catch (Exception e) {
           System.err.println("File not found");
           e.printStackTrace();
       }
       return noOfLinesCount;
   }

   public static void main(String[] args) {
       System.out.println("Total number of Commented lines are :: " + countLines("contents.txt"));
   }
}

contents.txt:

//London
China //Beijing
India - Delhi
//SaudiArabia
US //Wahsington

CODE SCREENSHOT WITH OUTPUT FOR ABOVE FILE CONTENTS:

Hope this will help you. Let me know if have any queries??..

Add a comment
Know the answer?
Add Answer to:
Using Java programming to answer this question. Write a method countLines(String inFilename) of a class FileUtility...
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
  • Please answer this question using Java programming. (a) Write a method textFilter(String binaryName, String textName) of...

    Please answer this question using Java programming. (a) Write a method textFilter(String binaryName, String textName) of a class Filters which reads the binary byte file with name binaryName, removes all non-printable bytes (which are bytes with values < 32 or > 127) and outputs the remaining bytes to an ASCII text file with name textName. Handle possible exception(s) by outputting a suitable message using the getMessage() method if an exception occurs. (b) What is the possible exception when the following...

  • Using Java, Write a method , getFirstLine, that is passed a String  argument and that returns the...

    Using Java, Write a method , getFirstLine, that is passed a String  argument and that returns the first line. (Recall that lines are terminated with the "\n" character .) Assume that the argument contains at least one complete, newline-terminated line.

  • JAVA: Write a program that inputs a string that represents a binary number. The string can...

    JAVA: Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console.

  • Java Programming Language Write code in Java that shows race condition using two or more threads....

    Java Programming Language Write code in Java that shows race condition using two or more threads. Give comments on first line (can be multiple lines), explain race condition that occurs in your program.

  • (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when...

    (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when a string is discoveredthat has too many characters in it. In the main driver of the program, read strings from the user until the user enters “DONE”. If a string is entered that has too many characters (say 20), throw, catch, and handle the exception. The exception is handled by printing an appropriate message, and the programwill continueprocessing more strings.

  • **JAVA** Write a binary search method for a String array that might contain nulls. Your method...

    **JAVA** Write a binary search method for a String array that might contain nulls. Your method should not crash or throw a runtime exception. See the driver program for examples. The method header is: public static int binarySearchWithNulls(String[] words, String target) must be recursive.

  • Java program please Exercise 7.2.8: Using the charAt) method, write and run a Java program that rea tring and prints th...

    Java program please Exercise 7.2.8: Using the charAt) method, write and run a Java program that rea tring and prints the string in reverse order. (Tip: Once the string has been entere aved, retrieve and display characters starting from the end of the string). Write a Java program that has 3 buttons. Clicking the first button shows the message "See no evil", clicking the second button shows the message "Hear no evil", clicking the third button shows the message "Speak...

  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

  • Given the following class: public class Vehicle { protected String manufacturer; // vehicle manufacturer protected int...

    Given the following class: public class Vehicle { protected String manufacturer; // vehicle manufacturer protected int year; // model year protected char fuelType; // G = gas, E = electric, D = diesel, W = wind, U = unknown protected String VIN; // an String to hold the 17 characters of the vehicle's VIN number. ... // no-arg constructor defined, as are all get/set methods. } Write me: An overloaded constructor (takes the four data members as parameters): assume that...

  • Write the code in python programming Language String Statistics: Write a program that reads a string...

    Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout 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