• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

BufferedWriter and incorrect data...

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create a file with BufferWriter and use 'myFile.write(small);' where small is an int of value 20000. when I do a BufferedReader on the same file I read back 63. If I increment my writes the file output increments although at some point soon the actual file is containing single character values and is moving up the ascii table(so that if I go and check my actual output.txt file I see'?', 'A', 'B','C', etc.) I guess my question is a very simple, how do I write and read an int value in files?

Thanks
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The single argument write() takes a single character as an argument; if you pass an int, it's truncated to the low 16 bits and interpreted as a character, as you've observed. To write a human-readable number, you have to (one way or another) convert it to a human-readable String and print that String. One way to do it is with the PrintWriter class, which will format and print many kinds of numbers and other values automatically.



This will create a file containing the characters '2', '0', '0', '0', '0', followed by the end-of-line sequence for your platform.
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic