File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2SE 5.0 and NIO

 
Todd Johnson
Ranch Hand
Posts: 61
  • 0
  • 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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic