The following code snippet shows you how to get the absolute path of a file. To do this we use the java.io.File
object getAbsolutePath()
method.
package org.kodejava.io;
import java.io.File;
public class AbsolutePathExample {
public static void main(String[] args) {
// Create an instance of File called file.
File file = new File("README.md");
// Now we want to know where is exactly this file is located in our file
// system. To do this we can use the getAbsolutePath() method of the File
// class.
String absolutePath = file.getAbsolutePath();
// Print out the JavaProgrammingBook.pdf location to the console.
System.out.println("AbsolutePath = " + absolutePath);
}
}
Here is the result of the program:
AbsolutePath = F:\Wayan\Kodejava\kodejava-example\README.md
Latest posts by Wayan (see all)
- How do I use the LongToDoubleFunction functional interface in Java? - March 15, 2025
- How do I use the LongSupplier functional interface in Java? - March 14, 2025
- How do I use the LongPredicate functional interface in Java? - March 14, 2025
I have tried this but got the path as
E:\Apache Software Foundation\Apache Tomcat 8.0.27\bin\Brandlogo.zip
Hi Safoora,
Can you show some codes? And what are you trying to achieve with your code?