• 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

Best Way to Write a Long to a File and Read a Long from a File

 
Ranch Hand
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to write a program that simply needs to write a single {long} value to a file, and I also need to write a program that will read a {long} value from a file. What's the best way to do this in Java?

Kevin S
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of file are you writing to? If a text file, you could try Formatter. And obviously for reading you could try a Scanner.
 
Kevin Simonson
Ranch Hand
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What sort of file are you writing to? If a text file, you could try Formatter. And obviously for reading you could try a Scanner.


It's completely up to me. It doesn't matter what type of file I write to, as long as I can read the same value back that I wrote to it.

After posting the original article I noticed classes {DataOutputStream} and {DataInputStream}, with their methods {writeLong()} and {readLong()} respectively. Is using these methods as good as using any other methods of any other class to write and read {long}s?

Kevin S
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the Java Tutorials section.
reply
    Bookmark Topic Watch Topic
  • New Topic