238

For new applications written in Java 7, is there any reason to use a java.io.File object any more or can we consider it deprecated?

I believe a java.nio.file.Path can do everything a java.io.File can do and more.

Roland
  • 7,525
  • 13
  • 61
  • 124
dogbane
  • 266,786
  • 75
  • 396
  • 414

8 Answers8

193

Long story short:

java.io.File will most likely never be deprecated / unsupported. That said, java.nio.file.Path is part of the more modern java.nio.file lib, and does everything java.io.File can, but generally in a better way, and more.

For new projects, use Path.

And if you ever need a File object for legacy, just call Path#toFile()

Migrating from File to Path

This Oracle page highlights differences, and maps java.io.File functionality to java.nio.file lib (including Path) functionality

Article by Janice J. Heiss and Sharon Zakhour, May 2009, discussing NIO.2 File System in JDK 7

Don Cheadle
  • 5,224
  • 5
  • 39
  • 54
  • 15
    You can read Oracle's comments on the differences here: http://docs.oracle.com/javase/tutorial/essential/io/legacy.html – Josiah Yoder Jan 26 '15 at 21:15
  • 4
    Also note that "Files" (in the plural) is **not** deprecated. It is essentially an abstract class that operates on Path objects and performs many of the features of the old File class, such as isDirectory() or exists() – Josiah Yoder Jan 26 '15 at 21:40
  • 4
    Now I'm wondering: why do the new File/FolderChooser dialogs in JavaFX 8 then still use `File` instead of `Path`? – piegames Sep 06 '17 at 12:06
  • 2
    Path is an interface. To create an instance, use Paths.get(filename). It could be because of the confusion of having to write Files.exists(Paths.get(filename)) instead of new File(filename).exists() that the older API is still used. – Josiah Yoder Jan 22 '18 at 18:27
  • `Path` can be more easily modified to "add children" with `resolve(...)` or "move up one level" with `getParent()`, etc. whereas `File` cannot. Essentially once you have finished modifying the Path, you'll often convert it `toFile()` so it can be sent into legacy methods such as a `FileInputStream` constructor. – MasterHD Feb 27 '20 at 13:24
17

can we consider it deprecated?

No, you can't consider it deprecated unless and until it is so marked in the File Javadoc.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 17
    Even if this is one of these "Because the RFC says so"-Answers, I would not consider it as a good answer. It's pretty obvious that File will be replace by Path. If you want to be ahead of time you can start using Path immediatly and use toFile() where needed. – Chris Jul 14 '13 at 00:31
  • 16
    @Chris Nothing has ever been removed from the JDK since they changed the AWT event model in 1.02. It isn't 'obvious' at all. It's wrong. – user207421 Apr 10 '14 at 18:20
  • 5
    @downvoters This answer is essentially a tautology. It can't be wrong. NB In the five years since I wrote this answer, Java 8 has subsequently appeared, and `java.io.File` is still neither removed nor even deprecated, and there is still nothing in the Javadoc to suggest that either of these things will ever happen. – user207421 Jan 13 '17 at 19:19
  • 2
    @EJP I just upvoted that comment of yours. However, I'm not entirely sure that you're right when you say the answer is a tautology. The question, which should probably have been squashed for being "opinion-based", is "can we **consider** it deprecated". Well, yes, the OP and anyone else **can**, but it isn't. – mike rodent Mar 05 '17 at 17:45
  • @mikerodent I suggest that's just a wilful misreading of what the question is really about. Also a partial quotation. – user207421 Jan 16 '18 at 23:46
  • @downvoter If you downvote tautologies do you also upvote self-contradictions? – user207421 Jan 16 '18 at 23:50
  • @EJP Sorry I would upvote. Bad manipulation on my smartphone. If you edit, I could reverse it. – davidxxx Jan 17 '18 at 07:57
  • @EJP Disagree. The question should have been squashed as "opinion-based". If the words "can we consider" had been absent it wouldn't have been posed in the first place. – mike rodent Jan 17 '18 at 08:54
  • @mikerodent I don't understand your final sentence. You don't know what the OP would and wouldn't have 'posed'. And I don't see that the presence of the word 'can' makes any substantive difference. – user207421 Feb 14 '18 at 01:06
11

Check this article about more info - http://www.oracle.com/technetwork/articles/javase/nio-139333.html

Basically file.Path will be the way to go from now on but as is widely known Java people tend to keep back-compatibility so I guess that's why they have left it.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
LordDoskias
  • 3,121
  • 3
  • 30
  • 44
  • Would you please update the link? I would like to read this article. – John B Jul 22 '13 at 15:15
  • Unfortunately I couldn't find the original article on the oracle web page. Here is a version from the wayback machine: http://web.archive.org/web/20090601091119/http://java.sun.com/developer/technicalArticles/javase/nio/ – LordDoskias Jul 22 '13 at 15:50
  • 1
    I found the article again on a normal Oracle side - added link to answer. – Duncan Jones Aug 20 '14 at 11:44
8

I will complete the very good answer of @mmcrae.

is there any reason to use a java.io.File object any more or can we consider it deprecated?

JDK classes are very rarely deprecated.
You can see on the the JDK 8 API deprecates list all classes deprecated since the first JDK.
It contains only a little part of classes that the Oracle documentation and the Java community discourage to use.
java.util.Date, java.util.Vector, java.util.Hashtable... that are classes with so many defects are not deprecated.
But why ?
Because conceptually something of deprecated means still there but discourage to use as it will very certainly be removed.
Thousands of programs rely on these bad designed classes.
For such classes, Java API developers will not give such a signal.

Answer of @EJP is so really right :

Not unless and until it is so marked in the Javadoc.

So, I think that your question would make more sense in its terms :
"As we have the choice, should we use java.io.File or java.nio.file.Path for new developments and if the answer is java.nio.file.Path, could you easily take advantage of java.io.File for legacy projects using java.io.File ?"

I believe a java.nio.file.Path can do everything a java.io.File can do and more.

You have the answer.

This oracle tutorial about legacy IO confirms your thinking.

Prior to the Java SE 7 release, the java.io.File class was the mechanism used for file I/O, but it had several drawbacks.

Many methods didn't throw exceptions when they failed, so it was impossible to obtain a useful error message. For example, if a file deletion failed, the program would receive a "delete fail" but wouldn't know if it was because the file didn't exist, the user didn't have permissions, or there was some other problem.

The rename method didn't work consistently across platforms. There was no real support for symbolic links.

More support for metadata was desired, such as file permissions, file owner, and other security attributes.

Accessing file metadata was inefficient.

Many of the File methods didn't scale. Requesting a large directory listing over a server could result in a hang. Large directories could also cause memory resource problems, resulting in a denial of service.

It was not possible to write reliable code that could recursively walk a file tree and respond appropriately if there were circular symbolic links.

With so many drawbacks for java.io.File, we need really no reason to use this class for new developments.
And even for legacy code using java.io.File, Oracle gives hints to use Path.

Perhaps you have legacy code that uses java.io.File and would like to take advantage of the java.nio.file.Path functionality with minimal impact to your code.

The java.io.File class provides the toPath method, which converts an old style File instance to a java.nio.file.Path instance, as follows:

Path input = file.toPath();

You can then take advantage of the rich feature set available to the Path class.

For example, assume you had some code that deleted a file:

file.delete();

You could modify this code to use the Files.delete method, as follows:

Path fp = file.toPath();
Files.delete(fp);
davidxxx
  • 125,838
  • 23
  • 214
  • 215
5

Yes, but many existing APIs, including Java7's own standard APIs, still work only with File type.

irreputable
  • 44,725
  • 9
  • 65
  • 93
3

Java.io.File is not deprecated. Yes java.nio.file.Path is better, but as long as there are still plenty of programs and text books using Java.io.File, if only for legacy reasons, it should not be considered deprecated, its too important. Doing so would just be throwing a spanner in the works for no over all gain. For example the Android framework uses File for some of its basic file handling features, many other things do to.

Andrew S
  • 2,847
  • 3
  • 33
  • 50
  • 1
    He didn't ask whether `Path` was better. He asked whether `File` was deprecated. – user207421 Feb 27 '18 at 09:10
  • 3
    @EJP I think you are being a little over pedantic. The OP did ask if java.io.File was deprecated and I answered that.. He also stated "I believe a java.nio.file.Path can do everything a java.io.File can do and more." I was merely confirming his comment, it was hardly worth a vote down. – Andrew S Apr 04 '18 at 05:27
-1

It is known that classes in java.nio package work with Path instances, and not File instances. It's recommended practice to work with the Path class if using java.nio wherever possible.

Now sometimes you will have to use the File class. That's because the method or constructor wants to File instance as a parameter, but when you do have a choice, make sure you use the Path over the File.

Maged Almaweri
  • 312
  • 4
  • 11
-10

For new applications written in Java 7, is there any reason to use a java.io.File object any more or can we consider it deprecated?

This is a bit like saying: "should Napoleon invade Russia, or are these Brussels sprouts really tasty?"

As to the second part of the question, you can indeed consider it deprecated. As of January 2018, it isn't deprecated. But there's nothing to stop you considering it so. Whether that will procure you any advantage in this life or the next is impossible to say.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • 7
    I don't understand your analogy. – Tunaki Mar 05 '17 at 18:03
  • Any "or" question should present two logical alternatives, both of which essentially answer the same question. – mike rodent Mar 05 '17 at 18:09
  • Sorry, this sounds highly pedantic in this context. The idea is "I want to use `File`. Should I, yes or no?" – Tunaki Mar 05 '17 at 18:11
  • No offence taken. The way the question is posed invites an analysis of what's it's "really" asking, and you've given a reasonable interpretation. But it is a loaded question. By pointing out its curious nature I'm just drawing attention to the fact that it should really have been squashed as "opinion-based". As to your question: "yes". You say you want to, so use it. The OP wanted someone to say "don't use `File`, it's a bit rubbish", but `File` is still with us five or more years later. Maybe we should celebrate that? – mike rodent Mar 05 '17 at 18:17
  • 1
    Yeah I agree it's a loaded question... especially since that a lot of existing 3rd party APIs still use `File` anyway. It's not going to die anytime soon. – Tunaki Mar 05 '17 at 18:22
  • 3
    `it isn't deprecated. But there's nothing to stop you *considering* it so` LOL. – Don Cheadle May 04 '17 at 21:02