Java: Path vs File

2020-01-25 04:03发布

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.

7条回答
smile是对你的礼貌
2楼-- · 2020-01-25 04:39

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

查看更多
神经病院院长
3楼-- · 2020-01-25 04:40

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.

查看更多
地球回转人心会变
4楼-- · 2020-01-25 04:46

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.

查看更多
家丑人穷心不美
5楼-- · 2020-01-25 04:50

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

查看更多
Lonely孤独者°
6楼-- · 2020-01-25 05:01

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.

查看更多
爷的心禁止访问
7楼-- · 2020-01-25 05:03

can we consider it deprecated?

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

查看更多
登录 后发表回答