I'm creating FileDialog and trying to get a FilePath for FileDialog object.
FileDialog fd = new FileDialog(this, "Open", FileDialog.LOAD);
fd.setVisible(true);
String path = ?;
File f = new File(path);
In this codes, I need to get a absolute FilePath for using with File object. How can I get filepath in this situation?
You can combine FileDialog.getDirectory() with FileDialog.getFile() to get a full path.
String path = fd.getDirectory() + fd.getFile();
File f = new File(path);
I needed to use the above instead of a call to File.getAbsolutePath() since getAbsolutePath() was returning the path of the current working directory and not the path of the file chosen in the FileDialog.
Check out File.getAbsolutePath()
:
String path = new File(fd.getFile()).getAbsolutePath();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With