I have a problem setting the file path in a Java FX application. I set the folder path to the "src" folder ( 'File file = new File ("src / hostbaze.txt");' ) and when i run it via netbeans it loads without any problems, everything works ok. The problem is when I run the application via the Java icon, via the Java application located in "C: \ Users \ almir \ Documents \ NetBeansProjects \ EEIPTV \ dist" ... then it can't load that file because the file is not in "dist" than in "src", and in "dist" this file cannot be saved.
Asked
Active
Viewed 72 times
-2
-
Consider [reading the data as a resource](https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder) rather than a file (if file is read-only). If you must read and write files, then consider [using either paths relative to the current working directory](https://stackoverflow.com/questions/4871051/how-to-get-the-current-working-directory-in-java) or paths specifically configured or chosen by a user. – jewelsea Aug 02 '21 at 21:47
-
I replaced the javafx tag with a java tag as this is a general Java question, not a JavaFX-specific question. – jewelsea Aug 02 '21 at 23:20
-
No `src` dir at runtime. – Abhijit Sarkar Aug 03 '21 at 00:54
-
Hi there, please take a look at [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and/or [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – 0x1C1B Aug 03 '21 at 06:35
1 Answers
0
If the file is updated externally, you can read a text file and give it a relative path like so:
new File("../src/hostbaze.txt");
However, be aware that this is a sketchy way of doing things because the file may not exist on other developers systems.
If the resource is needed for runtime, you can put it into the src/resources
folder and have it compiled with the source on compile-time, then read it with:
getClass().getResource("hostbaze.txt");

TravisF
- 459
- 6
- 13