如何获取 java applet 中的文件列表?

发布于 2024-12-26 01:18:28 字数 715 浏览 3 评论 0原文

我正在用Java开发一个协作项目,并且认为将它作为一个小程序嵌入到网页中是最简单的,然后每个人都可以添加他们的子类(奖金、敌人等),因为他们将它们放入各自的子类中文件夹。为了完成这项工作,我希望小程序检查网站上的文件夹,收集每个文件夹中的文件名称,然后将这些类名称放入一个数组中,因为它们都是从中随机生成的。

我最初尝试创建一个 File 对象并调用 .listFiles() 将其放入数组中,但文件对象无法识别我给它的路径,getCodeBase().toString()+"bonus.txt"< /code>,所以我尝试将文件夹中的所有类的名称放入 .txt 文件中,打开该文件,然后读取每一行。这在 eclipse 中可以工作,但是当我将 .class 和 .txt 文件上传到网站时,出现权限错误,因此我将类放入 .jar 中,自签名并再次上传,但小程序无法工作,它就冻结了。
任何时候我没有文件检索语句,无论是在 .jar 内还是在 .jar 外,小程序都可以正常工作,但如果我有这些代码行,它就会冻结或给出错误。

我尝试过 new Scanner(new file("bonus.txt")); 通过谷歌发现类似问题的缓冲阅读器的不同变体,以及通过 File 类加载地址的不同方法,但它们都不起作用。

我一直试图让小程序执行此操作,只是加载一个 .txt 文件(与 .class 文件位于同一目录中),读取内容并在小程序中打印它们,但没有成功。如果您能指出正确的方向,我将不胜感激。

I am working on a collaborative project in Java, and thought it would be easiest to have it as an applet embedded in a webpage, and then everyone can add their child classes (of bonuses, enemies, etc) as they make them into the respective folders. And to make this work, I wanted the applet to check the folders on the website, gather the names of the files in each folder, and then put those names of the classes into an array as they'd each be randomly generated from this.

I originally tried making a File object and calling .listFiles() to get it into an array, but the file object would not recognize the path I gave it, getCodeBase().toString()+"bonus.txt", so I tried just putting the names of all of the classes that would be in a folder in a .txt file, opening the file, and just reading each line. This would work in eclipse, but when I uploaded the .class and .txt files to the website, I got a permissions error, so i put the class into a .jar, self signed it and uploaded it again but the applet would not work, it just froze.
Any time I did not have a file retreval statement, in or out of a .jar, the applet would work just fine, but if I had those lines of code it would freeze or give an error.

I've tried new scanner(new file("bonus.txt"));
different variations of buffered readers found through google of similar issues, as well as different ways of loading the address through File class, but none of them have worked.

All I have been trying to get the applet to do this far is just load a .txt file (in the same directory as the .class file), read the contents and print them in the applet, with no success. Much appreciated if you could point me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2025-01-02 01:18:28

由(受信任的)小程序建立的文件将指向最终用户计算机的文件系统上的某个位置(该位置可能不存在)。 File 对象永远不能指向服务器,这不是它们的工作方式。

要从小程序访问资源,通常会使用 URL。 EG

URL urlToList = new URL(getCodeBase(), "bonus.txt");
Scanner scanner = new Scanner(urlToList.openStream());

请注意,这两行都不需要对小程序进行签名/信任。

A File established by a (trusted) applet will point to a location on the file-system of the end user's computer (which probably will not exist). File objects can never point back to the server, that is not how they work.

To access resources from an applet, you would generally use an URL instead. E.G.

URL urlToList = new URL(getCodeBase(), "bonus.txt");
Scanner scanner = new Scanner(urlToList.openStream());

Note that neither of those lines would require the applet to be signed/trusted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文