• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the best way to save an array in file?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How to deserialize array?I want to show members of deserialize array,?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Have problem in String = (names) in.readObject();
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Right?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic