Question
In Java Problem Description FileDisplay class Write a class named FileDisplay with the following methods: -Constructor. The class's constructor should take the name of the
In Java
Problem Description
FileDisplay class
Write a class named FileDisplay with the following methods:
-Constructor. The class's constructor should take the name of the file as an argument.
-displayHead. This method should only the first five lines of the file's contents. If the file contains less than five lines, it should display the file's entire content.
-displayContents. This method should display the entire contents of the file, the name of which was passed to the constructor.
-displayWithLineNumbers. This method should display the contents of the file, the name of which was passed to the constructor. Each line should be preceded with a line number followed by a colon. The line numbering should start at 1.
This is my code it compile but it gives me this message "Exception in thread "main" java.io.FileNotFoundException: input.txt (The system cannot find the file specified)" I need help
import java.io.BufferedReader;
import java.io.FileReader;
class FilePrint
{
private String fileName;
public FilePrint(String aFileName)
{
super();
fileName=aFileName;
}
public void displayHead()throws Exception
{
BufferedReader br=null;
br=new BufferedReader(new FileReader(fileName));
int no=1;
String line=br.readLine();
while(line!=null&&no<=5)
{
System.out.println(line);
line=br.readLine();
no++;
}
br.close();
}
public void displayContents()throws Exception
{
BufferedReader br=null;
br=new BufferedReader(new FileReader(fileName));
String line= br.readLine();
while(line!=null)
{
System.out.println(line);
line=br.readLine();
}
br.close();
}
public void displayWithLineNumbers()throws Exception
{
BufferedReader br=null;
br=new BufferedReader(new FileReader(fileName));
String line= br.readLine();
int n=1;
while(line!=null)
{
System.out.println(n+""+line);
line=br.readLine();
n++;
}
br.close();
}
}
public class TestFilePrint
{
public static void main(String[]args)throws Exception
{
FilePrint fp=new FilePrint("input.txt");
fp.displayContents();
System.out.println();
System.out.println();
fp.displayWithLineNumbers();
}
}
Step by Step Solution
3.46 Rating (159 Votes )
There are 3 Steps involved in it
Step: 1
Java code public class Name private String firstName private String lastName constructor public NameString firstName String lastName thisfirstName firstName thislastName lastName getters public String getFirstName return firstName public String getLastName return lastName setters public void setFirstNameString firstName thisfirstName firstName public void setLastNameString lastName thislastName lastName Override public String toString return firstName lastName public class Student protected Name name protected int id Constructor public StudentName name int id thisname name thisid id getters public Name ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started