As with most things, the answer is "it depends." What do you want to do with the file? What's in the array? You could serialization, you could simply loop over the array and print the elements as text, you could output objects as XML -- there are many possibilities. Which one is "best" depends on the application.
Software is all about trade offs. You need to balance speed, memory, complexity, time to code, ease to maintain, skill sets and many, many other variables. That requires you to decide which are most important.
If someone said "What is the best way to get from New York to Miami?", the answer would depend on whether speed, cost, comfort, or exercise is the most important. someone who wants to get into shape might say "a bicycle", someone who is worried about speed would say "A private chartered jet", and someone else who is concerned about comfort might say "a tour bus".
So, there is no "best" way to do anything that fits all situations.
Ernest Friedman-Hill wrote:You just use ObjectInputStream.readObject(), and cast the Object to a String[]. It's the opposite of what you've done here.
You didn't try to cast to String[] and probably that's why you are having that problem. However you've been around the forum long enough to know that "I have a problem" isn't very helpful. Please read this page from the FAQ: TellTheDetails.
abalfazl hossein wrote:The code works and print members of array. Do you have suggestion to improve it?
Quite a few. I'd use a "new-style" for loop for the printing, for example:
You don't need to close both the ObjectInputStream and the FileInputStream. In general, workign with Java streams, you only need to close the "outermost one" -- the one that wraps around the others. The others get closed automatically.
If there's a problem, your file doesn't get closed; always close files in a "finally" block.
Post by:autobot
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
a bit of art, as a gift, the permaculture playing cards