Java FileOutputStream: path relative to program fo

2020-02-14 04:13发布

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.

5条回答
Root(大扎)
2楼-- · 2020-02-14 04:49

To get the absolute path to a file use new File().getCanonicalFile().

new FileOutputStream(new File(".\\target\\dataset.xml").getCanonicalFile())
查看更多
老娘就宠你
3楼-- · 2020-02-14 04:51

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.

查看更多
▲ chillily
4楼-- · 2020-02-14 04:53

If you deploying as a jar, its possible to obtain the jar file name and path the current code is working in like this:

new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());

(from How to get the path of a running JAR file?)

查看更多
聊天终结者
5楼-- · 2020-02-14 04:54

Here you go:

String path = System.getProperty("user.dir");
查看更多
Fickle 薄情
6楼-- · 2020-02-14 04:58

To find relative path to current working directory say new File(".").

If you want to know absolute path of current working directory you can write new File(".").getAbsolutePath() or File(".").getAbsoluteFile()`.

I hope this answers your question. I am sorry if I did not understand you correctly.

查看更多
登录 后发表回答