The code you have new File("").getAbsolutePath()
is commonly used to determine the current directory from where you launched your application's main(String[])
method. So it looks like you are running from C:\Windows\System32
not on desktop folder.
The easiest way to fix your issue is to edit your application shortcut (see .lnk
file and set "Start in" folder), or cd
to be in an appropriate writable directory before running your application, or change your application to use file chooser to pick the target location for saves.
Alternatively pick sensible output paths for writing to instead of ""
, some good candidates are:
// Local user profile
File folder = new File(System.getenv("LOCALAPPDATA"), "myfolder");
// Roaming user profile
File folder = new File(System.getenv("APPDATA"), "myfolder");
// Temp folder:
File folder = new File(System.getProperty("java.io.tmpdir"), "myfolder")
Some sites suggest one of the following lines to locate Desktop
, but this is unreliable because it will not work if the folder has been moved or run on some non-English installations:
File maybeDesktop = new File(System.getProperty("user.home"), "Desktop");
Path maybeDesktop = Path.of(System.getProperty("user.home"), "Desktop");
If you want to read the exact location of user Desktop
from Java, you need to use JNI/JNA/Panama call to Windows API functions SHGetFolderPath
or SHGetKnownFolderPath
.