Question: IN JAVA!! 1 . Start Apache NetBeans . 2 . Create a new project called YourlastnameLab 1 2 with your last name . 3 .
IN JAVA!! Start Apache NetBeans Create a new project called YourlastnameLab with your last name Write code to create and write to an empty file.a Write a public static method called openFileForWriting that takes a String representing a file name and does the following: Create a new PrintWriter object using the given file name. If a FileNotFoundException is thrown, display an error message and exit the program. Otherwise, return the PrintWriter object. Make sure to add import statements for java.ioPrintWriter and java.ioFileNotFoundException.b Write code in the main function to test openFileForWriting. Use openFileForWriting to create and open a text file you can call it what you like, but it should have a txt file extension. Display a message to the user that the file was opened for writing. Use the PrintWriter object returned by openFileForWriting to write some lines to the file. Display a message to the user that output was written to the file. Close the file.c Test the program to see if it works. The output should look something like this: Opened file testfile.txt for writingWrote lines to testfile.txt To inspect the file, click the File menu, select Open File..., find the Lab folder in your workspace, click on the file you created, and click Open Write code to append to an existing file.d Write a public static method called openFileForAppending that takes a String representing a file name and does the following: Create a new PrintWriter object from a new FileOutputStream using the given file name, making sure to indicate that the file is being opened for appending. If a FileNotFoundException is thrown, display an error message and exit the program. Otherwise, return the PrintWriter object. Make sure to add an import statement for java.ioFileOutputStream.e Write code in the main function to test openFileForAppending. Use openFileForAppending to open the text file from the previous step. Display a message to the user that the file was opened for appending. Use the PrintWriter object returned by openFileForAppending to write some lines to the file. Display a message to the user that output was written to the file. Close the file.f Test the program to see if it works. The output should look something like this: Opened file testfile.txt for writingWrote lines to testfile.txtOpened file testfile.txt for appendingWrite lines to testfile.txt Check the file to see if the additional lines were added Write code to read from a file.g Write a public static method called openFileForReading that takes a String representing a file name and does the following: Create a new Scanner object using a new File object using the given file name. If a FileNotFoundException is thrown, display an error message and exit the program. Otherwise, return the Scanner object. Make sure to add an import statement for java.ioFile.h Write code in the main function to test openFileForReading. Use openFileForReading to open the text file from the previous two steps. Display a message to the user that the file was opened for reading. Use the Scanner object returned by openFileForReading to read and display all of the lines of the file. Display a message to the user that input was read from the file. Close the file.i Test the program to see if it works. The output should look something like this: Opened file testfile.txt for writingWrote lines to testfile.txtOpened file testfile.txt for appendingWrite lines to testfile.txtOpened file testfile.txt for readingfirst line of filesecond line of filethird line of filefourth line of fileRead lines from testfile.txt Make sure the file was not modified Write a method to read text from a file and display it to the screen.j Write a method called readLinesFromFile that takes a Scanner object as a parameter and does the following: Read each of the lines of the file and displays them. Keep track of the number of lines read and return the result.k Write code in the main method to test the readLinesFromFile method. Replace the code in the main method that displays the contents of the file with a call to readLinesFromFile. Use the value returned in the message that displays how many lines were read. Add code to open, read, display, and close the file after the code for writing lines to the empty file but before the code for appending lines to the file.l Test the program to see if it works. The output should look something like this: Opened file testfile.txt for writingWrote lines to testfile.txtOpened file testfile.txt for readingfirst line of filesecond line of fileRead lines from testfile.txtOpened file testfile.txt for appendingWrite lines to testfile.txtOpened file testfile.txt for readingfirst line of file second line of file third line of filefourth line of fileRead lines from testfile.txt Write a method to read from the keyboard and write it to a file.m Write a method called writeLinesToFile that takes a PrintWriter object as a parameter and does the following: Display a prompt to the user to enter lines of text to write to the file and to enter a blank line to stop entering text. Read lines from the keyboard and write them to a file until a blank line is entered. Keep track of the number of lines written and return the result.n Write code in the main method to test the writeLinesToFile method. Replace the code in the main method that writes lines to the empty file with a call to writeLinesToFile. Use the value returned in the message that displays how many lines were written. Do the same for the code that appends lines to the file.o Test the program to see if it works. The output should look something like this: Opened file testfile.txt for writingEnter the text you want to write to the file. Enter a blank line when you are done.'Twas brillig, and the slithy tovesDid gyre and gimble in the wabe:Wrote lines to testfile.txtOpened file testfile.txt for reading'Twas brillig, and the slithy tovesDid gyre and gimble in the wabe:Read lines from testfile.txtOpened file testfile.txt for appendingEnter the text you want to write to the file. Enter a blank line when you are done.All mimsy were the borogoves,And the mome raths outgrabe.Wrote lines to testfile.txtOpened file testfile.txt for reading'Twas brillig, and the slithy tovesDid gyre and gimble in the wabe:All mimsy were the borogoves,And the mome raths outgrabe.Read lines from testfile.txt Hand in your source file, YournameLabjava to the DL assignment folder called Lab Assignment
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
