Samuel Tober
Samuel Tober

Reputation: 43

Java: reaching a file without specifying directory

I have created a program that is creating and loading a Highscores.txt file. I want this program, when opened from a computer other than mine, to still be able to reach the txt file without having to save it in any specific directory. How could this be done?

Upvotes: 0

Views: 93

Answers (2)

bkakran
bkakran

Reputation: 1

  • You need a location from where you need to access the file. The location can be local or remote. Since you do not want to read the file from local file system , this means that file has to be accessed from remote location. File can be hosted in a webserver(HTTP GET) , FTP location , remote directory on a different machine and scp from that location , or save as blob in the database and load from it.

Upvotes: 0

Francisco Hernandez
Francisco Hernandez

Reputation: 2468

You can use

getClass().getClassLoader().getResource("Highscores.txt").getPath();

The unique condition for this is that the file have to be in the classpath, otherwise, this will be a NullPointerException

Upvotes: 1

Related Questions