如何在< default.package>中读取文本文件的内容[Java] [英] How to read the contents of a text file in <default.package> [Java]

查看:34
本文介绍了如何在< default.package>中读取文本文件的内容[Java]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果文本文件只是本地磁盘上的常规文件,我可以读取其内容.但是,如何读取导入到项目中的默认软件包中的文件?它没有常规意义上的文件路径,因此有点令人困惑.

Usually I can read the contents of a text file if it's just a regular file on my local disk. However, how can I read a file which exists in the default package I imported into my project? It doesn't have a filepath in the conventional sense so this is a little confusing.

推荐答案

访问jar中的文件时,您将无法再使用绝对路径.您必须使用在 Class 类上定义的 getResource (或 getResourceAsStream )方法.更准确地说,通话

When accessing a file within a jar, you cannot use absolute paths anymore. You must use the getResource (or getResourceAsStream) method defined on the Class class. More precisely, the call

MyClass.class.getResource(path)

将允许访问由 String 变量 path 标识的资源,该变量是从包含 MyClass 的包到资源的本地路径..创建软件包尤其是用于存储和访问资源(例如 myApp.data 软件包)是很常见的事情.

will give access to the resource identified by the String variable path, which is a local path from the package containing MyClass to the resource. It is a common thing to create packages especially to store and access resources (for example a myApp.data package).

这篇关于如何在< default.package>中读取文本文件的内容[Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆