• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

J2SE 5.0 and NIO  RSS feed

Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading Ivan Horton's Java 2 JDK 5 edition. I just went through the chapters on reading and writing files. He ony covers the new nio classes (FileChannels, ByteBuffers and the various view buffers) and states that he won't cover using streams directly because the new nio methodolgy will supercede using streams directly.

After learning the new nio stuff, I can see where this might be advantages to use for very complex file I/O. However this seems like it is very complex for simple file I/O.

Consider the example of having a files with a series of variable length strings seperated by a delimiter character (the pipe(|) for example). You want to write a program that goes through the file and pulls out each individual string. It's a pretty simply proposition to use a FileInputStream and StreamTokenizer to pull out the individual strings.

Using nio means using a FileChannel, ByteBuffer, and CharBuffer. It is much more complex. Also, because any individual string can be longer than your ByteBuffer (imagine a string that is 20K long), you have to implement your own additional buffering.

Is Ivan mistaken when he states that the new nio file i/o will supercede using streams directly? I can't imagine someone choosing to use nio instead of simple streams for the example given above. But if that was the case, then why would Ivor completely ignore file I/O using InputStreams, OutputStreams, Readers and Writers?
[ September 06, 2005: Message edited by: Todd Johnson ]
Java Cowboy
Posts: 16084
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Ivan Horton is mistaken if he really thinks that the NIO classes are intended as a replacement for the streams in the java.io package.

The NIO classes are good for high performance I/O and is block based (i.e., you read or write blocks of data at once), instead of stream based (where you read or write byte by byte).

It's overkill to use the NIO classes if you just want to read or write a small text file, for example, which is much easier using streams, readers and writers.
author and iconoclast
Posts: 24220
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's "Ivor" Horton, actually.

Be very careful about what you draw from this book. Have a look at the Amazon reviews for this edition to get an idea about how people have reacted to it. And I completely agree with Jesper about this NIO-vs-Streams pronouncement. java.nio is not intended as a replacement for java.io, but rather as an alternative in some situations.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!