2

Before Java 1.4 it was common practice to work with files by moving bytes around between different InputStreams/OutputStreams.

Since Java 1.4, where NIO got added, it is suggested to use Channels to do the same.

With NIO2 in Java 7, there will be yet another API in java.nio.file which supports doing things like

val source = Paths.get("fooDir/fooFile.txt")
val target = Paths.get("barDir/barFile.txt")
source moveTo target
source createLinkTo target

Are the older ones more or less useless now for file system operations unless you want to touch bytes manually?

soc
  • 27,983
  • 20
  • 111
  • 215
  • I suspect that NIO2 won't just replace the previous functionality, but instead extend it. I haven't checked, but I don't imagine you can do everything with NIO2 alone. – Peter Lawrey Mar 08 '11 at 11:15
  • As far as I understand, NIO2 does in fact replace quite a lot. For example, you no longer need `java.io.File`. – Thomas Mueller Mar 25 '11 at 06:50

3 Answers3

6

For most operations, NIO2 will let you do more / better.

Some operations are just impossible using legacy APIs (some attributes, ACL, file change notifications, better error handling...).

And best of all: this is not necessarily more difficult.

To answer your question: when you could do some operations with two different APIs, I don't see any use case where the old one would allow to do it better.

There has been some discussion:

Java NIO FileChannel versus FileOutputstream performance / usefulness http://mailinator.blogspot.com/2008/02/kill-myth-please-nio-is-not-faster-than.html

But I'd say newest APIs are designed to be faster. If they don't in some situation, expect a jvm update to restore the situation without having to change any code if you've been using the newer APIs.

Community
  • 1
  • 1
ymajoros
  • 2,454
  • 3
  • 34
  • 60
5

It is best practice to use the newer APIs when you can. They are usually better at handling corner cases like symlinks. They are also more likely to be built directly on OS primitives which will provide better hardware utilization. So the short answer to your question is "yes, the old ones are pretty much useless". The big downside to the new apis is that they require newer JREs to be installed.

Matt
  • 2,139
  • 1
  • 16
  • 20
5

I will add my 2 cents here. @Ymajoros and @Matt have summed it pretty well.

Definitely, the newer NIO will be better than the predecessors. There were lots of limitations in the old file io classes. I had moved from C++ back ground and found that even though the Apis were easier to use they were lacking lots of features. Right now also if you look at the classes, if you try to query remote directory you may see some slowness or your JVM may hang. This is being fixed in 7. Also to note is that some file systems support symbolic links and there is provision made to handle that. Iterators are being added for directory listing and also it will support the POSIX and the ACL control model.

allthenutsandbolts
  • 1,513
  • 1
  • 13
  • 34