• 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

Writing a file with a BufferedWriter and then reading it with a FileChannel

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a cross-post : stackoverflow

I am doing exercises in a Java book. The exercise is reading back a file containing a number of Strings (with no separater characters) from one file using the binary int lengths of the of the corresponding Strings from another file. I've verified that the files are written correctly by using the author's provided solution as well as my own for the writing class. I have gotten the reading class working by using a BufferedReader to read the strings. The first incarnation of the reader class used a FileChannel, and I'm not understanding why I can't get it working properly with the FileChannel. I'm thinking it has something to do with character encoding. The BufferedWriter is using "UTF-16", which is the default character encoding for the Java language.

Here is the output I am getting. For some reason I am getting a '?' at the beginning of the file which is causing the rest of the problems as you can see.

Length of saying: 44 Saying: ?A nod is as good as a wink to a blind horse
Length of saying: 27 Saying: .Least said, soonest mended
Length of saying: 42 Saying: .There are 10 kinds of people in the world
Length of saying: 50 Saying: .those that understand binary and those that don't
Length of saying: 47 Saying: .You can't amke a silk purse out of a sow's ear
Length of saying: 33 Saying: .Hindsight is always twnty-twenty
Length of saying: 29 Saying: .Existentialism has no future
Length of saying: 43 Saying: .Those who are not confused are misinformed
Length of saying: 32 Saying: .Better untaught that ill-taught

First, here is the code for the writing class which is generating the files:
(I'm aware of the inconsistencies in the code, such as magic numbers, etc. Just would like to get it working first.)



And here is the file for reading it using FileChannels for both files:

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic