Calm Sea
Calm Sea

Reputation: 181

get a file path

Is there a way to get the full path for a file exists on the computer ? For example , I want to get the full path for a file in a folder on desktop

I tried using :

  File f = new File("help.chm");

  String f2=f.getAbsolutePath();
  f3=f3.replaceAll("\\\\","/" );
  System.out.println("Path:"+f3);

but it gave me the path of the project like this:

C:/Users/toshiba/Documents/NetBeansProjects/test/help.chm

although the file is not located there .

Upvotes: 2

Views: 10531

Answers (5)

Tulains Córdova
Tulains Córdova

Reputation: 2639

You are attempting to read the file from (default folder)

C:/Users/toshiba/Documents/NetBeansProjects/test/

File doesn't exist but the would-be-file's path will be

C:/Users/toshiba/Documents/NetBeansProjects/test/

If you read the file from where it really is:

File f = new File("C:/Users/toshiba/Desktop/help.chm");

You will see that exists() returns true.

 System.out.println(f.exists());

Then:

  String f2=f.getCanonicalPath();

Upvotes: 0

sorifiend
sorifiend

Reputation: 6307

Since the other answers do not cover your question, here is my comment:

To get a file's path, you first need to tell your java program where it is or how to find it.

For your specific example you can get the desktop path using: System.getProperty("user.home") + "/Desktop"; then you can search through folders on your desktop for a matching file name.

Read here to learn how to search for files: docs.oracle.com/javase/tutorial/essential/io/find.html

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213193

If you create a file using new File("filename") which is the relative path, you cannot get the absolute path of the file using file.getAbsolutePath(), because the relative path is build according to the default user home directory or the JVM path.

Take a look at Java Doc: -

A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes.

A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

So, to get the absolute path for this case, you would actually have to write the path yourself. Get the absolute path till the directory where you saved the file, and append the file name to it.

Upvotes: 2

Zach Musgrave
Zach Musgrave

Reputation: 1022

The path "help.chm" will be relative to the directory from which you started the JVM, which in your case appears to be C:/Users/toshiba/Documents/NetBeansProjects/test/

To get a path to the desktop, you need to use the absolute path of the desktop directory in Windows, which will be something along the lines of C:/Users/toshiba/Desktop/help.chm

Upvotes: 0

Bohemian
Bohemian

Reputation: 424953

A File is a representation of a file path, not necessarily an existent file on disk - ie the file doesn't have to exist on disk for a File object to be not null.

That's why there's the File.exists() method.

Upvotes: 0

Related Questions