Home java What way is it better to specify the path to the file...

What way is it better to specify the path to the file in the Java program? Where is it better to store the files used by the program?




You can register the absolute path to the file, say like this:

image img = new imageicon ("g: \\ img.png"). GetImage ();

As far as I understand it, so do it all bad, because we do not know where the user will retain our program.

You can register the relative path to the file, say like this:

image img = new imageIcon ("img.png"). GetImage ();

But then (if only I correctly understood the experiments correctly) to get the full path to the JVM file makes the “concatenation” of the current directory with the relative way. Those. If I run the program such as a command:

G: \ Program \ Bin & gt; java -jar program.jar

The JVM will search for this location:

G: \ Program \ Bin \ img.png

As far as I understand, it is not bad for doing so, for we do not know which directory will run the user to our program.

You can do something in general interesting and difficult, such as this:

image img = new imageIcon (this.getclass (). GetResource ("img.png")) .getimage ( );

Here the file search will occur from the package where the class this.getclass () is located. (i.e., the most “concatenation” occurs with the way to the class package)

But probably badly interfere with class files with resource files?

or even so:

image img = new imageIcon (this.getclass (). getResource ("/ img.png")) .getimage ();

Here the file search occurs in the CLASSPATH directories. But in theory, then there may be several suitable files? And what will it be ?!

In general, questions:
Is that what I wrote?
What other ways exist?
When how to use?
What is the best way (if it exists)?

Answer 1, Authority 100%

The general answer to it will be just the word “depends” – the main question is what you work with. As a rule, Java applications are distributed in the form of jar archives, and if you have distributed with the resources (for example, icons), then they must either be there, among classes, or if you Distribute the application in the form of an archive and individual resources outside the archive, then you need to find the location of the JAR file and manually restore the path to the resource by analogy with Working Directory. In case you write a certain utility that processes arguments, you will most likely have to deal from absolute, and with relative ways – if you write, say, recycled images, then call Java -Jar Path / Resizer .jar image.png 300x400 will require a simple argument argument inwards directly, without any analysis at all.

in other words, all described methods have their use, and are more or less preferred depending on the situation; Each of them is not with something bad , but inapplicable in the current situation . If the question is to find the application resources, it is best either to throw in jar, or look for the place of installation of the application and count on it (because Working Dir may be any).


No one prevents you from using your images and other files in the same directory without an absolute path:

string path = new file (""). getabsolutepath ();

Store simply images, suppose in the IMG folder, the .jar file will be in the root folder. As a result, pull your images along the way path + "\ img" .

Similar theme (only with file creation) .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions