Java FileOutputStream:相对于程序文件夹的路径? [英] Java FileOutputStream: path relative to program folder?

查看:553
本文介绍了Java FileOutputStream:相对于程序文件夹的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个带有静态方法的类:

code> public static void saveToFile(String fileName)



当我用绝对路径调用它的时候,真正想要的是运行应用程序的位置和文件夹的相对路径。

我没有部署我的应用程序,但现在我想找到一个相对路径到(Netbeans)项目根目录以及一个名为data的文件夹: ProjectName \data\file.dat 。我应该使用 File 类还是把它变成一个 URI 或者什么的?



请注意,我更喜欢它是独立于系统的,并且在部署应用程序时仍然可以工作。最后,(相对)路径名将存储在属性文件中。



对不起,如果这个问题是重复的, / b>

解决方案

找到相对于java应用程序所在文件夹的路径的最佳方法是安装?

操作系统制造商已经说了很长一段时间了不把文件保存在应用程序目录。而是将 File 放置在 user.home 子目录 C>。用户主页是应该可以建立可以读取或写入的文件对象的地方。这也是一个可重复运行的平台,独立于平台。


请注意,我更喜欢它是系统无关的如果部署应用程序,则工作。这是一个简单的Java Swing应用程序。

然后,您应该考虑一个部署选项是 Java Web Start 。它不仅使最终用户的安装变得轻而易举(单击一个链接并确定提示),但它也提供了 PersistenceService 来存储少量的数据。这是一个小的演示。 PersistenceService



如果用例需要存储大量数据,服务将不适合数据本身,而是可以将(绝对)路径存储到实际数据存储的任何位置。这样,你可能有一个指向 user.home 默认 路径,但是如果用户选择了不同的位置,使用持久性服务存储该位置。



一个应用程序。使用JWS启动默认情况下具有安全沙箱。要写入本地文件系统需要放松沙箱。这就要求开发者对应用程序的Jars进行数字签名。 &安培;在启动文件中请求 all-permissions ,并在提示时让用户接受签名代码。



一边。 Sun在一段时间之前决定,使用保护域来查找缓存Jars的位置是一个安全风险。为此,他们决定返回的信息总是指向服务器,对于小应用程序& JWS应用程序。换句话说,保护域不会指向本地文件系统,即使是应用程序。缓存在本地。


What is the best way to find a path relative to the folder where a java application is "installed"?

I have a class with a static method: public static void saveToFile(String fileName)

When I call it with an absolute path, it works, but what I really want is the relative path to where the application is run from, and a folder.

I have not deployed my application, but right now I want to find a path relative to the (Netbeans) project root, and a folder within called data: ProjectName\data\file.dat. Should I use the File class or make it into a URI or something?

Note that I prefer it to be system-independent and will still work if the application is deployed. Eventually the (relative) pathname will be stored in a properties file.

Sorry if this question is a duplicate, any help is appreciated.

解决方案

What is the best way to find a path relative to the folder where a java application is "installed"?

OS manufacturers have been saying for a long time not to save files in the application directory. Instead put the File in a sub-directory of user.home. User home is where it should be possible to establish a file object that can be read or written. It is also a place that is reproducible across runs, and platform independent.

Note that I prefer it to be system-independent and will still work if the application is deployed. ..It's a simple Java Swing application..

Then one deployment option you should look into is Java Web Start. Not only does it make installation a breeze for the end-user (click a link & OK the prompts), but it also offers the PersistenceService to store small amounts of data. Here is a small demo. of the PersistenceService.

If the use-case requires storing large amounts of data, the persistence service will not be suitable for the data itself, but can instead store the (absolute) path to wherever the actual data is stored. That way, you might have a default path pointing to user.home, but if the user chooses a different location, store that location using the persistence service.

An app. launched using JWS has a security sand-box by default. To write to the local file-system requires that the sand-box be relaxed. That requires the developer to digitally sign the Jars of the app. & request all-permissions in the launch file, and for the user to accept the signed code when prompted.

As an aside. Sun decided some time ago that using the protection domain to find the location of the cached Jars was a security risk. For that reason, they decided that the information returned would always point to the server, for both applets & JWS apps. Or to put that another way, the protection domain will not point to the local file-system, even if the app. is cached locally.

这篇关于Java FileOutputStream:相对于程序文件夹的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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