![]() |
|
[Java] Java Assignment Help | Writing a Queue to a file - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Java Assignment Help | Writing a Queue to a file (/showthread.php?tid=6032562) |
Java Assignment Help | Writing a Queue to a file - GrandpaSwamp - Dec 13, 2019 Hello, I just haven't had much luck with queue's and writing to files. I have been just messing it up a lot. - save(): this will save the content of the queue to a file - load(): this will load the content of the queue from a file Here's the Java Class where it is contained https://pastebin.com/apu7HHd4 If someone could please help me out, that would be great! Thank you RE: Java Assignment Help - MavenGradle - Dec 13, 2019 I would like to help you out, but... What exactly is it that is keeping you from completing the assignment? It seems like you simply need someone to do your homework for you since you haven't written a single line of code yet. It's really hard to figure out what it is that you're struggling with. The entire thing? If so... You can simply save a Queue by reading out the data (poll()) and writing it in a specific format to a file. That format is entirely up to you. You could use a simple newline to split the values or write them to a json file as an array by serializing/de-serializing the data. Keep in mind that if you poll a Queue, that the value will be removed from said Queue. So, if you want to keep your original Queue intact, you might need to copy the queue that you want to write to a file. If you want to keep the collection of data intact, you should ideally look into other collection based objects such as a Map, List, etc to store your data in. Loading the data from a file back into the Queue is the exact same thing. In case you decided to split your values with a newline in a file, you can simply open a stream of the file and read it line by line and push the values into the Queue. You have to keep the following in mind as well: A default Queue in Java is first in, first out. RE: Java Assignment Help - GrandpaSwamp - Dec 13, 2019 (Dec 13, 2019 12:13 PM)MavenGradle Wrote: I would like to help you out, but... I am not struggling with the entire assignment. The entirety of the assignment is very big and it's only these two methods i can't figure out (these two methods are a very small portion of the assignment). If someone wanted to explain the best way to write a queue to a file, then that's wonderful, because, I simply have not been able to get it to work for the past day. Thank you for that explanation, My brain might just be fried, I don't know why i've been struggling with the queue so much. RE: Java Assignment Help - MavenGradle - Dec 13, 2019 (Dec 13, 2019 12:17 PM)GrandpaSwamp Wrote: I am not struggling with the entire assignment. The entirety of the assignment is very big and it's only these two methods i can't figure out (these two methods are a very small portion of the assignment). If someone wanted to explain the best way to write a queue to a file, then that's wonderful, because, I simply have not been able to get it to work for the past day. You're welcome. I did update my post so if you're interested in figuring it out yourself, there's more valuable information there. RE: Java Assignment Help | Writing a Queue to a file - programmer - Dec 14, 2019 You can save objects to files, make sure the class object implements Serializable and you can simply use ObjectOutputStream and ObjectInputStream RE: Java Assignment Help | Writing a Queue to a file - GenericSkid - Dec 14, 2019 (Dec 14, 2019 07:26 PM)programmer Wrote: You can save objects to files, make sure the class object implements Serializable and you can simply use ObjectOutputStream and ObjectInputStream Don't do this. Do not use Serializable unless you absolutely have to. From Oracle themselves, the serialization design was a "Horrible mistake" and its removal is even a long-term goal of the language developers. There are so many better approaches to choose from (Json/XML/CSV/etc.). Choosing one comes down to circumstantial use and personal preference. |