Dec 13, 2019 12:04 PM
(This post was last modified: Dec 18, 2019 04:56 PM by GrandpaSwamp.)
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
Self worth is my greatest enemy
Dec 13, 2019 12:13 PM
(This post was last modified: Dec 13, 2019 12:25 PM by MavenGradle.)
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.
(Dec 13, 2019 12:13 PM)MavenGradle Wrote: 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 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/deserializing the data.
Loading 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 in mind though, when reading and writing to /from a file that a default Queue in Java is first in, first out.
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.
Self worth is my greatest enemy
(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.
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.
You're welcome. I did update my post so if you're interested in figuring it out yourself, there's more valuable information there.
You can save objects to files, make sure the class object implements Serializable and you can simply use ObjectOutputStream and ObjectInputStream
Dec 14, 2019 09:09 PM
(This post was last modified: Dec 14, 2019 09:15 PM by GenericSkid.)
(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.
|