简体   繁体   中英

Trying to use Java file.getAbsolutePath() to get the absolute path of a file

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: 在此处输入图片说明

File is not an actual file, it is just a reference to a file path which may or may not exist. The file you created on your desktop has nothing to do with the File object you created in Java. The output is telling you, not where your expected file is, but where the File object's reference is going to go looking for it if you try to open the file.

To find an actual path to a file in an unknown location, you'll have search for it, as in " Recursively list files in Java ".

Since you say that the file is in your current location, File just adds that in front of it. If you execute your program in another directory, the path would be something else. Java does not check if the file exists and java can't know where you created a file named that - and there might be multiple files named so.

If you want to access a file that is not in your current work directory (you can see that using System.getProperty("user.dir) ), you need to give it a path in front of it - no matter if relative or absolute, but java can't guess where your file is.

The easiest way to do this for any file whether it be on the desktop or wherever would be to first browse for the file. Second, select the file. Third, use code to get the absolute file path of the file you selected.

To do that, you can use something like this:

JFileChooser choose = new JFileChooser();
choose.showOpenDialog(null);
File f = choose.getSelectedFile();
String filePath = f.getAbsolutePath();

Otherwise, IF AND ONLY IF your file is located on the desktop or if the desktop is somewhere in the file path, then you can use this:

File f = new File(System.getProperty("user.home") + "/Desktop" + "\\file.txt");
if(f.exists())
   String filePath = f.getAbsolutePath(); 

Hope this helps!!

This code goes directly to the desktop and then gets the file you hardcoded in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM