Categories: java, macos, filepath

Path for a file on a Mac without the username in Java

1 answer

I want to be able to open a text file with some basic instruction from within a program in Java, What I currently have to open the file:

Desktop desktop = Desktop.getDesktop(); File file = new File(/* File Path, this is what I am concerned with. */); if(file.exists()) try {; } catch (IOException e) {e.printStackTrace();} 

This works fine, what I have been using for the path is:


The reason I have a problem is because I want to send my program to a friend so that he can try it out. The current solution I have is to have a text field that the user can input their username into and then the path is:

"/Users/" + username + "/Documents/text.txt" (username being the username that is inputed into the text field)

Where else could I store the file or what else could I use for a path? Is there a way to find the user automatically? Should I store the file in a folder made in the root?

All answers to this question, which has the identifier 61078070

The best answer:

If you like to use a fixed path under the user folder, you can get the username via

String username = System.getProperty(""); 

Alternatively you can use a path relative to the location of the program, for example Paths.get("").toAbsolutePath(), see Getting the Current Working Directory in Java

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data