1

Possible Duplicate:
Java 7: Path vs File

I am learning about Java file handling. I am reading a book which says that Java 7 has NIO, with the Path class, and that we should use that in preference to the File class.

Are Path and File classes the same?

Community
  • 1
  • 1
srisar
  • 1,495
  • 5
  • 18
  • 27

1 Answers1

3

The File (and other classes in java.io.* are rather old, going back to the early days of Java. They work and they do the job, but they aren't easy to use. The File class itself is an encapsulation of a file's filesystem path in addition to certain metadata (read-only status, file size).

Many Java IO operations and classes, such as FileInputStream work with the old File class, but most of them also accept filenames as String instances.

The Path class is a better-designed and modern alternative. You can read up about it here: http://openjdk.java.net/projects/nio/javadoc/java/nio/file/Path.html

While Path is new, the File class is not officially deprecated or obsoleted. I think it's one of those "if it's a new project, use X, otherwise keep on using Y"-things.

So in response to your question: no, they are not the same. Path replaces File and should be used in new projects.

Dai
  • 141,631
  • 28
  • 261
  • 374