I'm working on a simple project, and i'm trying to get the absolute path of a file. This is what i'm trying:
String s = "file.txt";
System.out.println(new File(s).getAbsoluteFile().getAbsolutePath());
The output is:
C:\Users\Marcello\Desktop\Java Workspace\Simple Project\file.txt
Where "Simple Project" is the name of the project where the code is.
But the output should be:
C:\Users\Marcello\Desktop\file.txt
That is where i created the .txt
file.
I also tryed with file.getCanonicalFile()
and "file.getCanonicalPath()". And i've had the same issue.
In this project i'm using a text file, but i want it to work with folders too. So if someone knows a solution also for folder, i'd like to see it.
Sorry for my english but it's not my mother-tongue, thanks in advance.
EDITED:
I think the absolute path should be:
C:\Users\Marcello\Desktop\file.txt
Because i created the .txt
file by right-clicking on the desktop....
Here is a screenshot of my desktop: