- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should...

50.1K

Verified Solution

Question

Programming

- Create a java class named SaveFile in which write thefollowing:

Constructor: The class's constructor should take the name of afile as an argument

A method save (String line): This method should open the filedefined by the constructor, save the string value of line at theend of the file, and then close the file.

- In the same package create a new Java class and it DisplayFilein which write the following:

Constructor: The class's constructor should take the name of afile as an argument

A method display(): This method should check if the file definedby the constructor  exist or not, if the file does notexist it displays the message \"The file does not exists\" and thenexit the program, however if the file exists it displays the filecontents line by line into the screen, and then close the file.

A method display (int n): This method is similar to display ()but it displays the first n number of lines of the file's contentsonly. If the file contains less than n lines, it should display thefile's entire contents.

A method display (int from, int to): This method is similar todisplay() but it displays the files's contents starting from linenumber from to the line number to. If the file contains less thanto lines, it should display the file's contents. Note that, from isalways less than to.

- In the same package, create another class called FilesDemo, init's main method do the following:

create an instance of the class SaveFile to create new filenamed \"lines.txt\"

print the following lines into the file:

1-Lorem ipsum dolor sit amet

2-Consectetuer adipiscing elit

3-Sed diam nonummy nibh euismod tincidunt

4-Ut wisi enim ad minim veniam

5-Quis nostrud exerci tation ullamcorper

6-Suscipit lobortis nisl ut aliquip ex ea commodo consequat

7-Duis autem vel eum iriure dolor in hendrerit

8-Vel illum dolore eu feugiat nulla facilisis at vero eros

Create an instance of the class DisplayFile to open\"lines.txt\"

using this instance invoke the method display()

using this instance invoke the method display(3) anddisplay(10)

using this instance invoke the method display(3, 5)

Run your program and make sure it prints the correct output.

Answer & Explanation Solved by verified expert
4.0 Ratings (456 Votes)
SaveFilejavaimport javaioFileimport javaioFileWriterimport javaioIOExceptionimport javaioPrintWriterpublic class SaveFile private String fileNamepublic SaveFileString fileNamethisfileName fileNamepublic void saveString lineFileWriter fwPrintWriter pwtry open the file in append modefw new FileWriternew FilethisfileName truepw new PrintWriterfwpwwriteline    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students