Java中的资源、URI、URL、路径和文件有什么区别?

时间:2022-02-16 13:21:36

I'm looking at a piece of Java code right now, and it takes a path as a String and gets its URL using URL resource = ClassLoader.getSystemClassLoader().getResource(pathAsString);, then calls String path = resource.getPath() and finally executes new File(path);.

我现在正在查看一段Java代码,它将路径作为字符串,并使用URL resource = ClassLoader.getSystemClassLoader(). getresource (pathAsString);然后调用String path = resource.getPath(),最后执行新的文件(path);

Oh, and there are also calls to URL url = resource.toURI(); and String file = resource.getFile().

噢,还有对URL URL = resource.toURI()的调用;和String file = resource.getFile()。

I'm totally confused right now - mostly because of the terminology, I guess. Can someone please walk me through the differences, or provide a few links to Dummy-proof material? Especially URI to URL and Resource to File? To me, it feels like they should be the same thing, respectively...

我现在完全搞不懂了——我想主要是因为术语吧。谁能给我介绍一下其中的不同之处,或者给我提供一些关于不透明材料的链接?尤其是URL的URI和文件的资源?对我来说,他们应该分别是同一件事……

The difference between getFile() and getPath() is explained here: What's the difference between url.getFile() and getpath()? (Interestingly they both seem to return Strings, which probably adds a whole lot to my state of mind...)

getFile()和getPath()之间的区别在这里说明:url.getFile()和getPath()之间的区别是什么?(有趣的是,它们似乎都返回了字符串,这可能给我的思维状态增加了很多……)

Now, if I have a locator that references a class or package in a jar file, will those two (i.e. path an file strings) differ?

现在,如果我有一个在jar文件中引用类或包的定位器,那么这两个(即路径文件字符串)会有所不同吗?

resource.toString() would give you jar:file:/C:/path/to/my.jar!/com/example/, after all (note the exclamation mark).

tostring()将为您提供jar:file:/C:/path/to/my.jar!/ com/example/终究(注意惊叹号)。

Is the difference between URI and URL in Java that the former doesn't encode spaces? Cf. Files, URIs, and URLs conflicting in Java (This answer explains the general, conceptual difference between the two terms fairly well: URIs identify and URLs locate;)

URI和URL在Java中的区别在于前者不编码空格吗?Cf.文件、uri和url在Java中相互冲突(这个答案解释了两个术语之间的一般概念差异:uri标识和url定位;)

Lastly - and most importantly - why do I need File object; why isn't a Resource (URL) enough? (And is there a Resource object?)

最后——也是最重要的——为什么我需要File object;为什么资源(URL)不够?(有资源对象吗?)

Sorry if this question is a bit unorganized; it just reflects the confusion I have... :)

不好意思,如果这个问题没有条理;它只是反映了我的困惑……:)

5 个解决方案

#1


36  

UPDATE 2017-04-12 Check JvR's answer as it contains more exhaustive and exact explanation!

更新2017-04-12查看JvR的答案,因为它包含了更详尽和准确的解释!


Please note that I do not consider myself 100% competent to answer, but nevertheless here are some comments:

请注意,我认为我不能百分之百地胜任回答,但是这里有一些评论:

  • File represents a file or directory accessible via file system
  • 文件表示通过文件系统访问的文件或目录。
  • resource is a generic term for a data object which can be loaded by the application
    • usually resources are files distributed with the application / library and loaded via class-loading mechanism (when they reside on class-path)
    • 通常资源是与应用程序/库一起分布并通过类加载机制加载的文件(当它们驻留在类路径上时)
  • 资源是可以被应用程序加载的数据对象的通用术语,通常资源是与应用程序/库一起分布并通过类加载机制加载的文件(当它们驻留在类路径上时)
  • URL#getPath is getter on the path part of URL (protocol://host/path?query)
  • URL#getPath是URL路径部分的getter(协议://主机/路径?查询)
  • URL#getFile as per JavaDoc returns path+query
  • URL#getFile根据JavaDoc返回路径+查询

In Java, URI is just a data structure for manipulating the generic identifier itself.

在Java中,URI只是用于操作通用标识符本身的数据结构。

URL on the other hand is really a resource locator and offers you features to actually read the resource via registered URLStreamHandlers.

另一方面,URL实际上是一个资源定位器,它提供了通过注册的URLStreamHandlers读取资源的特性。

URLs can lead to file-system resources and you can construct URL for every file system resource by using file:// protocol (hence File <-> URL relation).

URL可以导致文件系统资源,您可以使用file:// protocol(因此文件<-> URL关系)为每个文件系统资源构造URL。

Also be aware that that URL#getFile is unrelated to java.io.File.

还要注意,这个URL#getFile与java. io.s文件无关。


Why do I need File object; why isn't a Resource (URL) enough?

为什么我需要文件对象;为什么资源(URL)不够呢?

It is enough. Only if you want to pass the resource to some component which can work only with files, you need to get File from it. However not all resource URLs can be converted to Files.

这就足够了。只有当您想要将资源传递给某个只能处理文件的组件时,才需要从该组件获取文件。但是并不是所有的资源url都可以转换成文件。

And is there a Resource object?

有资源对象吗?

From the JRE point of view, it's just a term. Some frameworks provide you with such class (e.g. Spring's Resource).

从JRE的观点来看,这只是一个术语。有些框架为您提供此类类(例如Spring的资源)。

#2


25  

I'm totally confused right now - mostly because of the terminology, I guess. Can someone please walk me through the differences, or provide a few links to Dummy-proof material? Especially URI to URL and Resource to File? To me, it feels like they should be the same thing, respectively...

我现在完全搞不懂了——我想主要是因为术语吧。谁能给我介绍一下其中的不同之处,或者给我提供一些关于不透明材料的链接?尤其是URL的URI和文件的资源?对我来说,他们应该分别是同一件事……

The terminology is confusing and sometimes befuddling, and mostly born from the evolution both of Java as an API and as a platform over time. To understand how these terms came to mean what they do, it is important to recognise two things that influence Java's design:

这个术语让人困惑,有时也让人迷惑不解,它主要源于Java作为API的演化,以及随着时间的推移而形成的平台。要理解这些术语是如何产生它们的含义,有两点很重要:

  • Backwards compatibility. Old applications should run on newer installations, ideally without modification. This means that an old API (with its names and terminology) needs to be maintained through all newer versions.
  • 向后兼容性。旧的应用程序应该在更新的安装上运行,理想情况下不需要修改。这意味着旧的API(及其名称和术语)需要通过所有更新的版本进行维护。
  • Cross-platform. The API should provide a usable abstraction of its underlying platform, whether that be an operating system or a browser.
  • 跨平台的。API应该提供其底层平台的可用抽象,无论是操作系统还是浏览器。

I'll walk through the concepts and how they came to be. I'll answer your other, specific questions after that, because I may have to refer to something in the first part.

我将介绍这些概念以及它们是如何形成的。之后我会回答你的其他具体问题,因为我可能要在第一部分提到一些东西。

What is a "Resource"?

An abstract, generic piece of data that can be located and read. Loosely said, Java uses this to refer to a "file" that may not be a file but does represent a named piece of data. It does not have a direct class or interface representation in Java, but because of its properties (locatable, readable) it is often represented by an URL.

可定位和读取的抽象的、通用的数据块。松散地说,Java使用它来引用一个“文件”,这个“文件”可能不是一个文件,但确实表示一个已命名的数据块。它在Java中没有直接的类或接口表示,但是由于它的属性(可定位的,可读的),它通常由URL表示。

Because one of Java's early design goals was to be run inside of a browser, as a sandboxed application (applets!) with very limited rights/privileges/security clearance, Java makes a clear (theoretical) difference between a file (something on the local file system) and a resource (something it needs to read). This is why reading something relative to the application (icons, class files, and so on) is done through ClassLoader.getResource and not through the File class.

因为Java早期的设计目标之一是在浏览器中运行,作为一个沙箱应用程序(applet !这就是为什么要通过ClassLoader来读取与应用程序相关的内容(图标、类文件等等)。getResource,而不是通过File类。

Unfortunately, because "resource" is also a useful generic term outside of this interpretation, it is also used to name very specific things (e.g. class ResourceBundle, UIResource, Resource) that are not, in this sense, a resource.

不幸的是,由于“资源”也是这个解释之外的一个有用的通用术语,它也被用于命名非常特定的事物(例如类ResourceBundle、UIResource、resource),在这个意义上,它们不是资源。

The main classes representing (a path to) a resource are java.nio.file.Path, java.io.File, java.net.URI, and java.net.URL.

表示资源(路径)的主要类是java.nio.file。路径,io。文件、java.net.URI和java.net.URL。

File (java.io, 1.0)

An abstract representation of file and directory pathnames.

文件和目录路径名的抽象表示。

The File class represents a resource that is reachable through the platform's native file system. It contains only the name of the file, so it is really more a path (see later) that the host platform interprets according to its own settings, rules, and syntax.

File类表示可以通过平台的本机文件系统访问的资源。它只包含文件的名称,因此它实际上更像是一个路径(参见后面),该主机平台根据它自己的设置、规则和语法进行解释。

Note that File doesn't need to point to something local, just something that the host platform understands in the context of file access, e.g. a UNC path in Windows. If you mount a ZIP file as a file system in your OS, then File will read its contained entries just fine.

注意,文件不需要指向本地的东西,只是主机平台在文件访问上下文中理解的东西,例如Windows中的UNC路径。如果将ZIP文件作为文件系统挂载到您的操作系统中,那么文件将读取它所包含的条目。

URL (java.net, 1.0)

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine.

类URL表示一个统一的资源定位符,一个指向万维网上“资源”的指针。资源可以是简单的文件或目录,也可以是对更复杂对象的引用,例如对数据库或搜索引擎的查询。

In tandem with the concept of a resource, the URL represents that resource the same way the File class represents a file in the host platform: as a structured string that points to a resource. URL additionally contains a scheme that hints at how to reach the resource (with "file:" being "ask the host platform"), and so allows pointing at resources through HTTP, FTP, inside a JAR, and whatnot.

与资源的概念一起,URL代表了资源,就像文件类表示主机平台中的文件一样:作为指向资源的结构化字符串。URL还包含一个方案,提示如何访问资源(带有“file:”作为“询问主机平台”),因此允许通过HTTP、FTP、JAR等方式指向资源。

Unfortunately, URLs come with their own syntax and terminology, including the use of "file" and "path". In case the URL is a file-URL, URL.getFile will return a string identical to the path string of the referenced file.

不幸的是,url有它们自己的语法和术语,包括使用“file”和“path”。如果URL是文件URL, URL。getFile将返回与引用文件的路径字符串相同的字符串。

Class.getResource returns an URL: it is more flexible than returning File, and it has served the needs of the system as imagined in the early 1990's.

类。getResource返回一个URL:它比返回文件更灵活,它满足了系统在20世纪90年代早期的需求。

URI (java.net, 1.4)

Represents a Uniform Resource Identifier (URI) reference.

表示统一资源标识符(URI)引用。

URI is a (slight) abstraction over URL. The difference between URI and URL is conceptual and mostly academic, but URI is better defined in a formal sense, and covers a wider range of use cases. Because URL and URI are/were not the same thing, a new class was introduced to represent them, with methods URI.toURL and URL.toURI to move between one and the other.

URI是URL的(轻微)抽象。URI和URL之间的差异是概念性的,而且主要是学术性的,但是URI在正式意义上定义得更好,并且涵盖了更广泛的用例。因为URL和URI不是一回事,所以引入了一个新的类来表示它们,并使用方法URI。toURL和URL。图里在一个和另一个之间移动。

In Java, the main difference between URL and URI is that an URL carries the expectation of being resolvable, something the application might want an InputStream from; an URI is treated more like an abstract thingamajig that might point to something resolvable (and usually does), but what it means and how to reach it are more open to context and interpretation.

在Java中,URL和URI的主要区别在于URL预期是可解析的,应用程序可能需要输入流;URI更像是一个抽象的东西,它可能指向一些可解决的东西(通常是这样),但是它的含义以及如何达到它,对上下文和解释更加开放。

Path (java.nio.file, 1.7)

An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.

可以用来在文件系统中定位文件的对象。它通常表示与系统相关的文件路径。

The new file API, iconified in the Path interface, allows for much greater flexibility than the File class could offer. The Path interface is an abstraction of the File class, and is part of the New IO File API. Where File necessarily points to a "file" as understood by the host platform, Path is more generic: it represents a file (resource) in an arbitrary file system.

新的文件API(在Path接口中进行了iconified)允许比file类提供的更大的灵活性。路径接口是文件类的抽象,是新的IO文件API的一部分。当文件必须指向主机平台所理解的“文件”时,路径更通用:它表示任意文件系统中的文件(资源)。

Path takes away the reliance on the host platform's concept of a file. It could be an entry in a ZIP file, a file reachable through FTP or SSH-FS, a multi-rooted representation of the application classpath, or really anything that can be meaningfully represented through the FileSystem interface and its driver, FileSystemProvider. It brings the power of "mounting" file systems into the context of a Java application.

路径消除了对主机平台的文件概念的依赖。它可以是ZIP文件中的一个条目,可以通过FTP或SSH-FS访问的文件,可以是应用程序类路径的多根表示,也可以是通过文件系统接口及其驱动程序FileSystemProvider有效地表示的任何内容。它将“安装”文件系统的能力引入到Java应用程序的上下文中。

The host platform is represented through the "default file system"; when you call File.toPath, you get a Path on the default file system.

主机平台通过“默认文件系统”表示;当你调用文件。toPath,你在默认文件系统上有一个路径。


Now, if I have a locator that references a class or package in a jar file, will those two (i.e. path an file strings) differ?

现在,如果我有一个在jar文件中引用类或包的定位器,那么这两个(即路径文件字符串)会有所不同吗?

Unlikely. If the jar file is on the local file system, you should not have a query component, so URL.getPath and URL.getFile should return the same result. However, pick the one you need: file-URLs may not typically have query components, but I could sure add one anyway.

不太可能的。如果jar文件在本地文件系统上,则不应该有查询组件URL。getPath和URL。getFile应该返回相同的结果。但是,选择您需要的一个:文件- url通常不具有查询组件,但是我可以肯定地添加一个。

Lastly - and most importantly - why do I need File object; why isn't a Resource (URL) enough?

最后——也是最重要的——为什么我需要File object;为什么资源(URL)不够呢?

URL might not be enough because File gives you access to housekeeping data such as permissions (readable, writable, executable), file type (am I a directory?), and the ability to search and manipulate the local file system. If these are features you need, then File or Path provide them.

URL可能不够,因为文件可以让您访问管理数据,比如权限(可读、可写、可执行)、文件类型(我是一个目录吗?),以及搜索和操作本地文件系统的能力。如果这些是您需要的特性,那么文件或路径提供它们。

You don't need File if you have access to Path. Some older API may require File, though.

如果可以访问路径,则不需要文件。不过,一些较老的API可能需要文件。

(And is there a Resource object?)

(有资源对象吗?)

No, there isn't. There are many things named like it, but they are not a resource in the sense of ClassLoader.getResource.

不,没有。有很多东西是这样命名的,但是它们不是类加载器的资源。

#3


11  

Pavel Horal's answer is nice.

Pavel Horal的回答很好。

As he says, the word "file" has totally different (practically unrelated) meanings in URL#getFile vs java.io.File - may be that's part of the confusion.

正如他所说,在URL#getFile vs . java.io中,“file”一词的含义完全不同(实际上是不相关的)。文件-可能这是混淆的一部分。

Just to add:

添加:

  • A resource in Java is an abstract concept, a source of data that can be read. The location (or address) of a resource is represented in Java by a URL object.

    Java中的资源是一个抽象的概念,可以读取的数据源。资源的位置(或地址)在Java中由URL对象表示。

  • A resource can correspond to a regular file in the local filesystem (specifically, when its URL begins with file://). But a resource is more general (it can be also some file stored in a jar, or some data to be read from the network, or from memory, or...). And it's also more limited, because a File (besides being other things than a regular file: a directory, a link) can also be created and writen to.

    资源可以与本地文件系统中的常规文件相对应(具体地说,当它的URL以file://开头时)。但是资源更一般(它也可以是存储在jar中的文件,也可以是要从网络、内存或…中读取的数据)。它也更有限,因为一个文件(除了普通文件之外,还可以创建和写入一个目录、一个链接)。

  • Remember in Java a File object does not really represents "a file" but the location (the full name, with path) of a file. So, a File object allows you to locate (and open) a file, as a URLallows you to access (and open) a resource. (There is no Resource class in Java to represent a resource, but neither there is one to represent a file! once more : File is not a file, it's the path of a file).

    请记住,在Java中,文件对象并不是真正表示“一个文件”,而是表示文件的位置(全名,带有路径)。因此,File对象允许您定位(并打开)一个文件,而url允许您访问(并打开)一个资源。(Java中没有表示资源的资源类,但也没有表示文件的资源类!再说一遍:文件不是文件,它是文件的路径)。

#4


3  

As I understand them, you could categorize them as following:

根据我对他们的理解,你可以将他们分为以下几类:

Web-Based: URIs and URLs.

网络:uri和url。

  • URLs: An URL is a definite location on the internt (just a normal webaddress like - *.com)
  • URL: URL是internt上的一个确定的位置(只是一个普通的网址,如- *.com)
  • URIs: Ever URL is an URI. But URIs can also contain things like "mailto:", so they are also, well some what of a "script" I'd say.
  • 永远URL是一个URI。但是uri也可以包含“mailto:”之类的内容,所以它们也是,嗯,我想说的是“脚本”。

And local: Resource, Path and Files

本地:资源、路径和文件。

  • Resource: Resources are files inside your jar. They are used to load files out of jars / containers.
  • 资源:资源是jar中的文件。它们用于从jar /容器中加载文件。
  • Path: A path is basically a string. But it comes with some handy functions to concatenate multiple strings, or add files to a string. It makes sure the path you are building is valid.
  • 路径:路径基本上就是一个字符串。但是它附带了一些方便的函数来连接多个字符串,或者将文件添加到一个字符串中。它确保您正在构建的路径是有效的。
  • File: This is a reference to a directory or file. It's used to modify files, open them etc.
  • 文件:这是对目录或文件的引用。它用于修改文件,打开文件等等。

It would be easier if they would be merged into one class - they are really confusing :D

如果将它们合并到一个类中会更容易些——它们确实令人困惑:D

I hope this helps you :)

我希望这对你有帮助。

(I just took a look at the documentation - look at docs.oracle.com)

(我只是看了一下文档——看一下docs.oracle.com)

#5


0  

A File is an abstract representation of an entity in the local filesystem.

文件是本地文件系统中实体的抽象表示。

A path is generally a string indicating the location of a file within a file system. It usually doesn't include the filename. So c:\documents\mystuff\stuff.txt would have a path with the value of of "C:\documents\mystuff" Obviously the format of absolute filenames and paths would vary enormously from filesystem to filesystem.

路径通常是表示文件系统中文件位置的字符串。它通常不包含文件名。所以c:\ mystuff \ \文件的东西。txt会有一个值为“C:\documents\mystuff”的路径,显然,从文件系统到文件系统,绝对文件名和路径的格式会有很大的不同。

URL is a susbset of URI with URL usually representing resources accessible over http. I don't think there is any sort of ironclad rule about when something has to be a URI vs a URL. URIs are strings in the form of "protocol://resource-identifier" such as bitcoin://params, http://something.com?param=value. Classes like URL generally wrap the string and provide utility methods that String would have no reason to supply.

URL是URI的susbset, URL通常表示可以通过http访问的资源。我不认为有什么严格的规则规定什么东西必须是URI而不是URL。uri是以“协议://资源标识符”的形式表示的字符串,如比特币://params, http://something.com?像URL这样的类通常封装字符串并提供实用方法,而这些方法是字符串没有理由提供的。

No such thing as Resource, at least not in the sense you're talking about. Just because a method is named getResource doesn't mean it returns an object of type Resource.

没有所谓的资源,至少在你所说的意义上没有。仅仅因为一个方法被命名为getResource,并不意味着它返回一个类型为Resource的对象。

Ultimately the best way to figure out what a Class's methods do is to create an instance of it in your code, call the methods and then either step through in debug mode or send the results to System.out.

最终了解类的方法的最佳方法是在代码中创建类的实例,调用这些方法,然后在调试模式中逐步执行,或者将结果发送到System.out。

#1


36  

UPDATE 2017-04-12 Check JvR's answer as it contains more exhaustive and exact explanation!

更新2017-04-12查看JvR的答案,因为它包含了更详尽和准确的解释!


Please note that I do not consider myself 100% competent to answer, but nevertheless here are some comments:

请注意,我认为我不能百分之百地胜任回答,但是这里有一些评论:

  • File represents a file or directory accessible via file system
  • 文件表示通过文件系统访问的文件或目录。
  • resource is a generic term for a data object which can be loaded by the application
    • usually resources are files distributed with the application / library and loaded via class-loading mechanism (when they reside on class-path)
    • 通常资源是与应用程序/库一起分布并通过类加载机制加载的文件(当它们驻留在类路径上时)
  • 资源是可以被应用程序加载的数据对象的通用术语,通常资源是与应用程序/库一起分布并通过类加载机制加载的文件(当它们驻留在类路径上时)
  • URL#getPath is getter on the path part of URL (protocol://host/path?query)
  • URL#getPath是URL路径部分的getter(协议://主机/路径?查询)
  • URL#getFile as per JavaDoc returns path+query
  • URL#getFile根据JavaDoc返回路径+查询

In Java, URI is just a data structure for manipulating the generic identifier itself.

在Java中,URI只是用于操作通用标识符本身的数据结构。

URL on the other hand is really a resource locator and offers you features to actually read the resource via registered URLStreamHandlers.

另一方面,URL实际上是一个资源定位器,它提供了通过注册的URLStreamHandlers读取资源的特性。

URLs can lead to file-system resources and you can construct URL for every file system resource by using file:// protocol (hence File <-> URL relation).

URL可以导致文件系统资源,您可以使用file:// protocol(因此文件<-> URL关系)为每个文件系统资源构造URL。

Also be aware that that URL#getFile is unrelated to java.io.File.

还要注意,这个URL#getFile与java. io.s文件无关。


Why do I need File object; why isn't a Resource (URL) enough?

为什么我需要文件对象;为什么资源(URL)不够呢?

It is enough. Only if you want to pass the resource to some component which can work only with files, you need to get File from it. However not all resource URLs can be converted to Files.

这就足够了。只有当您想要将资源传递给某个只能处理文件的组件时,才需要从该组件获取文件。但是并不是所有的资源url都可以转换成文件。

And is there a Resource object?

有资源对象吗?

From the JRE point of view, it's just a term. Some frameworks provide you with such class (e.g. Spring's Resource).

从JRE的观点来看,这只是一个术语。有些框架为您提供此类类(例如Spring的资源)。

#2


25  

I'm totally confused right now - mostly because of the terminology, I guess. Can someone please walk me through the differences, or provide a few links to Dummy-proof material? Especially URI to URL and Resource to File? To me, it feels like they should be the same thing, respectively...

我现在完全搞不懂了——我想主要是因为术语吧。谁能给我介绍一下其中的不同之处,或者给我提供一些关于不透明材料的链接?尤其是URL的URI和文件的资源?对我来说,他们应该分别是同一件事……

The terminology is confusing and sometimes befuddling, and mostly born from the evolution both of Java as an API and as a platform over time. To understand how these terms came to mean what they do, it is important to recognise two things that influence Java's design:

这个术语让人困惑,有时也让人迷惑不解,它主要源于Java作为API的演化,以及随着时间的推移而形成的平台。要理解这些术语是如何产生它们的含义,有两点很重要:

  • Backwards compatibility. Old applications should run on newer installations, ideally without modification. This means that an old API (with its names and terminology) needs to be maintained through all newer versions.
  • 向后兼容性。旧的应用程序应该在更新的安装上运行,理想情况下不需要修改。这意味着旧的API(及其名称和术语)需要通过所有更新的版本进行维护。
  • Cross-platform. The API should provide a usable abstraction of its underlying platform, whether that be an operating system or a browser.
  • 跨平台的。API应该提供其底层平台的可用抽象,无论是操作系统还是浏览器。

I'll walk through the concepts and how they came to be. I'll answer your other, specific questions after that, because I may have to refer to something in the first part.

我将介绍这些概念以及它们是如何形成的。之后我会回答你的其他具体问题,因为我可能要在第一部分提到一些东西。

What is a "Resource"?

An abstract, generic piece of data that can be located and read. Loosely said, Java uses this to refer to a "file" that may not be a file but does represent a named piece of data. It does not have a direct class or interface representation in Java, but because of its properties (locatable, readable) it is often represented by an URL.

可定位和读取的抽象的、通用的数据块。松散地说,Java使用它来引用一个“文件”,这个“文件”可能不是一个文件,但确实表示一个已命名的数据块。它在Java中没有直接的类或接口表示,但是由于它的属性(可定位的,可读的),它通常由URL表示。

Because one of Java's early design goals was to be run inside of a browser, as a sandboxed application (applets!) with very limited rights/privileges/security clearance, Java makes a clear (theoretical) difference between a file (something on the local file system) and a resource (something it needs to read). This is why reading something relative to the application (icons, class files, and so on) is done through ClassLoader.getResource and not through the File class.

因为Java早期的设计目标之一是在浏览器中运行,作为一个沙箱应用程序(applet !这就是为什么要通过ClassLoader来读取与应用程序相关的内容(图标、类文件等等)。getResource,而不是通过File类。

Unfortunately, because "resource" is also a useful generic term outside of this interpretation, it is also used to name very specific things (e.g. class ResourceBundle, UIResource, Resource) that are not, in this sense, a resource.

不幸的是,由于“资源”也是这个解释之外的一个有用的通用术语,它也被用于命名非常特定的事物(例如类ResourceBundle、UIResource、resource),在这个意义上,它们不是资源。

The main classes representing (a path to) a resource are java.nio.file.Path, java.io.File, java.net.URI, and java.net.URL.

表示资源(路径)的主要类是java.nio.file。路径,io。文件、java.net.URI和java.net.URL。

File (java.io, 1.0)

An abstract representation of file and directory pathnames.

文件和目录路径名的抽象表示。

The File class represents a resource that is reachable through the platform's native file system. It contains only the name of the file, so it is really more a path (see later) that the host platform interprets according to its own settings, rules, and syntax.

File类表示可以通过平台的本机文件系统访问的资源。它只包含文件的名称,因此它实际上更像是一个路径(参见后面),该主机平台根据它自己的设置、规则和语法进行解释。

Note that File doesn't need to point to something local, just something that the host platform understands in the context of file access, e.g. a UNC path in Windows. If you mount a ZIP file as a file system in your OS, then File will read its contained entries just fine.

注意,文件不需要指向本地的东西,只是主机平台在文件访问上下文中理解的东西,例如Windows中的UNC路径。如果将ZIP文件作为文件系统挂载到您的操作系统中,那么文件将读取它所包含的条目。

URL (java.net, 1.0)

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine.

类URL表示一个统一的资源定位符,一个指向万维网上“资源”的指针。资源可以是简单的文件或目录,也可以是对更复杂对象的引用,例如对数据库或搜索引擎的查询。

In tandem with the concept of a resource, the URL represents that resource the same way the File class represents a file in the host platform: as a structured string that points to a resource. URL additionally contains a scheme that hints at how to reach the resource (with "file:" being "ask the host platform"), and so allows pointing at resources through HTTP, FTP, inside a JAR, and whatnot.

与资源的概念一起,URL代表了资源,就像文件类表示主机平台中的文件一样:作为指向资源的结构化字符串。URL还包含一个方案,提示如何访问资源(带有“file:”作为“询问主机平台”),因此允许通过HTTP、FTP、JAR等方式指向资源。

Unfortunately, URLs come with their own syntax and terminology, including the use of "file" and "path". In case the URL is a file-URL, URL.getFile will return a string identical to the path string of the referenced file.

不幸的是,url有它们自己的语法和术语,包括使用“file”和“path”。如果URL是文件URL, URL。getFile将返回与引用文件的路径字符串相同的字符串。

Class.getResource returns an URL: it is more flexible than returning File, and it has served the needs of the system as imagined in the early 1990's.

类。getResource返回一个URL:它比返回文件更灵活,它满足了系统在20世纪90年代早期的需求。

URI (java.net, 1.4)

Represents a Uniform Resource Identifier (URI) reference.

表示统一资源标识符(URI)引用。

URI is a (slight) abstraction over URL. The difference between URI and URL is conceptual and mostly academic, but URI is better defined in a formal sense, and covers a wider range of use cases. Because URL and URI are/were not the same thing, a new class was introduced to represent them, with methods URI.toURL and URL.toURI to move between one and the other.

URI是URL的(轻微)抽象。URI和URL之间的差异是概念性的,而且主要是学术性的,但是URI在正式意义上定义得更好,并且涵盖了更广泛的用例。因为URL和URI不是一回事,所以引入了一个新的类来表示它们,并使用方法URI。toURL和URL。图里在一个和另一个之间移动。

In Java, the main difference between URL and URI is that an URL carries the expectation of being resolvable, something the application might want an InputStream from; an URI is treated more like an abstract thingamajig that might point to something resolvable (and usually does), but what it means and how to reach it are more open to context and interpretation.

在Java中,URL和URI的主要区别在于URL预期是可解析的,应用程序可能需要输入流;URI更像是一个抽象的东西,它可能指向一些可解决的东西(通常是这样),但是它的含义以及如何达到它,对上下文和解释更加开放。

Path (java.nio.file, 1.7)

An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.

可以用来在文件系统中定位文件的对象。它通常表示与系统相关的文件路径。

The new file API, iconified in the Path interface, allows for much greater flexibility than the File class could offer. The Path interface is an abstraction of the File class, and is part of the New IO File API. Where File necessarily points to a "file" as understood by the host platform, Path is more generic: it represents a file (resource) in an arbitrary file system.

新的文件API(在Path接口中进行了iconified)允许比file类提供的更大的灵活性。路径接口是文件类的抽象,是新的IO文件API的一部分。当文件必须指向主机平台所理解的“文件”时,路径更通用:它表示任意文件系统中的文件(资源)。

Path takes away the reliance on the host platform's concept of a file. It could be an entry in a ZIP file, a file reachable through FTP or SSH-FS, a multi-rooted representation of the application classpath, or really anything that can be meaningfully represented through the FileSystem interface and its driver, FileSystemProvider. It brings the power of "mounting" file systems into the context of a Java application.

路径消除了对主机平台的文件概念的依赖。它可以是ZIP文件中的一个条目,可以通过FTP或SSH-FS访问的文件,可以是应用程序类路径的多根表示,也可以是通过文件系统接口及其驱动程序FileSystemProvider有效地表示的任何内容。它将“安装”文件系统的能力引入到Java应用程序的上下文中。

The host platform is represented through the "default file system"; when you call File.toPath, you get a Path on the default file system.

主机平台通过“默认文件系统”表示;当你调用文件。toPath,你在默认文件系统上有一个路径。


Now, if I have a locator that references a class or package in a jar file, will those two (i.e. path an file strings) differ?

现在,如果我有一个在jar文件中引用类或包的定位器,那么这两个(即路径文件字符串)会有所不同吗?

Unlikely. If the jar file is on the local file system, you should not have a query component, so URL.getPath and URL.getFile should return the same result. However, pick the one you need: file-URLs may not typically have query components, but I could sure add one anyway.

不太可能的。如果jar文件在本地文件系统上,则不应该有查询组件URL。getPath和URL。getFile应该返回相同的结果。但是,选择您需要的一个:文件- url通常不具有查询组件,但是我可以肯定地添加一个。

Lastly - and most importantly - why do I need File object; why isn't a Resource (URL) enough?

最后——也是最重要的——为什么我需要File object;为什么资源(URL)不够呢?

URL might not be enough because File gives you access to housekeeping data such as permissions (readable, writable, executable), file type (am I a directory?), and the ability to search and manipulate the local file system. If these are features you need, then File or Path provide them.

URL可能不够,因为文件可以让您访问管理数据,比如权限(可读、可写、可执行)、文件类型(我是一个目录吗?),以及搜索和操作本地文件系统的能力。如果这些是您需要的特性,那么文件或路径提供它们。

You don't need File if you have access to Path. Some older API may require File, though.

如果可以访问路径,则不需要文件。不过,一些较老的API可能需要文件。

(And is there a Resource object?)

(有资源对象吗?)

No, there isn't. There are many things named like it, but they are not a resource in the sense of ClassLoader.getResource.

不,没有。有很多东西是这样命名的,但是它们不是类加载器的资源。

#3


11  

Pavel Horal's answer is nice.

Pavel Horal的回答很好。

As he says, the word "file" has totally different (practically unrelated) meanings in URL#getFile vs java.io.File - may be that's part of the confusion.

正如他所说,在URL#getFile vs . java.io中,“file”一词的含义完全不同(实际上是不相关的)。文件-可能这是混淆的一部分。

Just to add:

添加:

  • A resource in Java is an abstract concept, a source of data that can be read. The location (or address) of a resource is represented in Java by a URL object.

    Java中的资源是一个抽象的概念,可以读取的数据源。资源的位置(或地址)在Java中由URL对象表示。

  • A resource can correspond to a regular file in the local filesystem (specifically, when its URL begins with file://). But a resource is more general (it can be also some file stored in a jar, or some data to be read from the network, or from memory, or...). And it's also more limited, because a File (besides being other things than a regular file: a directory, a link) can also be created and writen to.

    资源可以与本地文件系统中的常规文件相对应(具体地说,当它的URL以file://开头时)。但是资源更一般(它也可以是存储在jar中的文件,也可以是要从网络、内存或…中读取的数据)。它也更有限,因为一个文件(除了普通文件之外,还可以创建和写入一个目录、一个链接)。

  • Remember in Java a File object does not really represents "a file" but the location (the full name, with path) of a file. So, a File object allows you to locate (and open) a file, as a URLallows you to access (and open) a resource. (There is no Resource class in Java to represent a resource, but neither there is one to represent a file! once more : File is not a file, it's the path of a file).

    请记住,在Java中,文件对象并不是真正表示“一个文件”,而是表示文件的位置(全名,带有路径)。因此,File对象允许您定位(并打开)一个文件,而url允许您访问(并打开)一个资源。(Java中没有表示资源的资源类,但也没有表示文件的资源类!再说一遍:文件不是文件,它是文件的路径)。

#4


3  

As I understand them, you could categorize them as following:

根据我对他们的理解,你可以将他们分为以下几类:

Web-Based: URIs and URLs.

网络:uri和url。

  • URLs: An URL is a definite location on the internt (just a normal webaddress like - *.com)
  • URL: URL是internt上的一个确定的位置(只是一个普通的网址,如- *.com)
  • URIs: Ever URL is an URI. But URIs can also contain things like "mailto:", so they are also, well some what of a "script" I'd say.
  • 永远URL是一个URI。但是uri也可以包含“mailto:”之类的内容,所以它们也是,嗯,我想说的是“脚本”。

And local: Resource, Path and Files

本地:资源、路径和文件。

  • Resource: Resources are files inside your jar. They are used to load files out of jars / containers.
  • 资源:资源是jar中的文件。它们用于从jar /容器中加载文件。
  • Path: A path is basically a string. But it comes with some handy functions to concatenate multiple strings, or add files to a string. It makes sure the path you are building is valid.
  • 路径:路径基本上就是一个字符串。但是它附带了一些方便的函数来连接多个字符串,或者将文件添加到一个字符串中。它确保您正在构建的路径是有效的。
  • File: This is a reference to a directory or file. It's used to modify files, open them etc.
  • 文件:这是对目录或文件的引用。它用于修改文件,打开文件等等。

It would be easier if they would be merged into one class - they are really confusing :D

如果将它们合并到一个类中会更容易些——它们确实令人困惑:D

I hope this helps you :)

我希望这对你有帮助。

(I just took a look at the documentation - look at docs.oracle.com)

(我只是看了一下文档——看一下docs.oracle.com)

#5


0  

A File is an abstract representation of an entity in the local filesystem.

文件是本地文件系统中实体的抽象表示。

A path is generally a string indicating the location of a file within a file system. It usually doesn't include the filename. So c:\documents\mystuff\stuff.txt would have a path with the value of of "C:\documents\mystuff" Obviously the format of absolute filenames and paths would vary enormously from filesystem to filesystem.

路径通常是表示文件系统中文件位置的字符串。它通常不包含文件名。所以c:\ mystuff \ \文件的东西。txt会有一个值为“C:\documents\mystuff”的路径,显然,从文件系统到文件系统,绝对文件名和路径的格式会有很大的不同。

URL is a susbset of URI with URL usually representing resources accessible over http. I don't think there is any sort of ironclad rule about when something has to be a URI vs a URL. URIs are strings in the form of "protocol://resource-identifier" such as bitcoin://params, http://something.com?param=value. Classes like URL generally wrap the string and provide utility methods that String would have no reason to supply.

URL是URI的susbset, URL通常表示可以通过http访问的资源。我不认为有什么严格的规则规定什么东西必须是URI而不是URL。uri是以“协议://资源标识符”的形式表示的字符串,如比特币://params, http://something.com?像URL这样的类通常封装字符串并提供实用方法,而这些方法是字符串没有理由提供的。

No such thing as Resource, at least not in the sense you're talking about. Just because a method is named getResource doesn't mean it returns an object of type Resource.

没有所谓的资源,至少在你所说的意义上没有。仅仅因为一个方法被命名为getResource,并不意味着它返回一个类型为Resource的对象。

Ultimately the best way to figure out what a Class's methods do is to create an instance of it in your code, call the methods and then either step through in debug mode or send the results to System.out.

最终了解类的方法的最佳方法是在代码中创建类的实例,调用这些方法,然后在调试模式中逐步执行,或者将结果发送到System.out。