Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.File vs java.nio.Files which is the preferred in new code?

While writing answers around SO, a user tried pointing out that java.io.File should not be used in new code, instead he argues that the the new object java.nio.Files should be used instead; he linked to this article.

Now I have been developing in Java for several years now, and have not heard this argument before; since reading his post I have been searching, and have not found many other sources that confirm this, and personally, I feel like many of the points argued in the article are weak and that if you know how to read them, errors thrown by the File class will generally tell you exactly what the issue is.

As I am continually developing new code my question is this:

Is this an active argument in the Java community? Is Files preferred over File for new code? What are the major advantages / disadvantages between the two?

like image 671
Matt Clark Avatar asked Aug 21 '15 15:08

Matt Clark


People also ask

Is Java io file deprecated?

It is not deprecated because it is not broken. Or more precisely, because the Java team and/or Oracle management do not think it is sufficiently broken to warrant the level of disruption and pushback that deprecation of File would cause1.

What is the use of NIO in Java?

Java NIO enables you to do non-blocking IO. For instance, a thread can ask a channel to read data into a buffer. While the channel reads data into the buffer, the thread can do something else. Once data is read into the buffer, the thread can then continue processing it.

What is Java NIO files?

Java For Testers Java NIO package provide one more utility API named as Files which is basically used for manipulating files and directories using its static methods which mostly works on Path object.


2 Answers

The documentation that you linked give the answer:

The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems. This API may be used to overcome many of the limitations of the java.io.File class. The toPath method may be used to obtain a Path that uses the abstract path represented by a File object to locate a file. The resulting Path may be used with the Files class to provide more efficient and extensive access to additional file operations, file attributes, and I/O exceptions to help diagnose errors when an operation on a file fails.

like image 186
Jens Avatar answered Oct 04 '22 16:10

Jens


File has a newer implementation: Path. With a builder Paths.get("..."). And Files has many nice utility functions with better implementations too (move instead of the sometimes failing File.renameTo).

A Path maintains its file system. Hence you can copy out of a zip file system ("jar:file:..... .zip") some path to another file system and vice versa.

File.toPath() may help an incremental transition.

The utilities alone in Files make a move to the newer classes profitable.

like image 34
Joop Eggen Avatar answered Oct 04 '22 15:10

Joop Eggen