can someone help me with writing the code in java for reading from a file and writing to file. File I/O (Read Textbook Appendix E
can someone help me with writing the code in java for reading from a file and writing to file.
File I/O (Read Textbook Appendix E for more instructions)
Open the class entitled FileIO as it is. You will see the class is already set up for you (don't worry that it doesn't compile yet, it will when you add your code). All of the code you will write will go into a try block. Also, you do not need to write a test class for this class. Download the file grocerylist.txt Download grocerylist.txt Place the "grocerylist.txt" file in your root directory. When you look at your project folder's structure, the file should be on the same level as the src and bin folders. After adding it, you may need to refresh your project file structure by hitting F5 or right clicking the project and choosing Refresh. As with any class we use, remember you can look up the File, Scanner and PrintWriter class online, by searching something like "java 8 Scanner." The APIs will have much more information than you need for this lab. Reading from a file Create a File object. The constructor for File will take a single parameter of type String that will contain the String filename parameter. Now create a new GroceryBag object, which will hold the groceries you will read off of the text document. Create a Scanner object. Scanners constructor will take the File object you just created as a parameter. This gives the Scanner object access to the file. Scanner has a method called hasNext that will return true as long as the end of the document has not been reached. Use this in a while loop to scan the text document until the end of the document has been reached. After each call to hasNext, use Scanners method next (which takes no parameters) to retrieve the next String object from the text document. Add the String object to the GroceryBag. Once all the Strings have been scanned in using the Scanner, close the Scanner object using the close method, which takes no parameters. This prevents any unintentional access of the text document.
Writing to a file
Now create a PrintWriter object. A PrintWriter object allows you to write to a text document. PrintWriters constructor takes a single parameter of type String which will represent the name you want to give your text file.PrintWriter will create the text file for you when it's created. Call the document for your solution: output.txt. PrintWriter has a method called print that takes a single parameter of type String which prints that String to the text document you created. Use the bags toString method and print the resulting string to the text document you are writing to. Then close the PrintWriter object the same way you close the Scanner object. Catch block: Your catch block can look like this: catch (FileNotFoundException e) { System.out.println("Scanner error opening the file "); System.out.println(e.getMessage()); }
Verify your program works
Run the program (similarly to running a JUnit test, but instead of the option for JUnit test, you should see the option to run as a Java Application). A quick way to find output.txt is to go to the Lab 04 folder in the Package Explorer in Eclipse, then click F5, which refreshes the contents of Lab 04. Open Lab 04 and you will see output.txt at the bottom of Lab 04's contents. Double click to open the file and you should see toString's representation of the bag with the items you scanned into it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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