• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the best way to save an array in file?  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the best way to save an array in file?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How to deserialize array?I want to show members of deserialize array,?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just use ObjectInputStream.readObject(), and cast the Object to a String[]. It's the opposite of what you've done here.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Have problem in String = (names) in.readObject();
 
Paul Clapham
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Right?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:Right?

Why are you asking us? What happened when you ran it?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code works and print members of array. Do you have suggestion to improve it?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!