Java File example to get filepath of the file using getPath
By candid | Posted :
Nov 25, 2016
| Updated :
Nov 25, 2016
public String getPath()
Converts this abstract pathname into a pathname string. The resulting string uses the default name-separator character to separate the names in the name sequence.
Program:
package com.candidjava;
import java.io.File;
public class FileGetpath
{
public static void main(String[] args)
{
File f = null;
String v;
boolean bool = false;
try
{
// create new file
f = new File("test.txt");
// pathname string from abstract pathname
v = f.getPath();
// true if the file path exists
bool = f.exists();
// if file exists
if (bool)
{
// prints
System.out.print("pathname: " + v);
}
}
catch (Exception e)
{
// if any error occurs
e.printStackTrace();
}
}
}
Output:
pathname: test.txt
Description:
The relative path of the file is being fetched and shown above.