There are multiple ways to get the file path for a file in java, let's see a few examples,
Example 1: Using File class
import java.io.File;
/**
* Get File Path using File class.
*/
public class FilePathExample {
public static void main(String[] args) {
File file = new File("data.txt");
String filePath = file.getAbsolutePath();
System.out.println("File Path: " + filePath);
}
}
Output:
Example 2: Using File class
Path path = Paths.get("data.csv");
String absolutePath = path.toAbsolutePath().toString();
Example 3: Using ClassLoader
public static void main(String[] args) throws URISyntaxException {
ClassLoader classLoader = FilePathExample.class.getClassLoader();
URL resource = classLoader.getResource("data.txt");
if (resource != null) {
Path path = Paths.get(resource.toURI());
System.out.println("File path: " + path);
} else {
System.out.println("File not found");
}
}
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java Jackson ObjectMapper Class with Examples
- How to add a Delay of a Few Seconds in Java Program
- Java SE JDBC Select Statement Example
- How to Word-Warp Console logs in IntelliJ
- Convert Java Array to ArrayList Code Example
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed.
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- How to extract Java Jar/War/Ear files in Linux
- Java: Convert String to Binary
- Java Generics: Type parameter cannot be instantiated directly
- How to Create and Run a Command Line Application using Spring Boot
- How to Convert a Java Object into an Optional with Example
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to Set JAVA_HOME PATH to Mac .zshrc profile file
- Formatting Double in Java [Examples]
- Java 8: Stream map with Code Examples
- Fix: SyntaxError: ( was never closed [Python]
- Java Split String by Spaces
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database
- Fix: SpringFramework BeanDefinitionValidationException: Could not find an init method named
- Java Date Time API: LocalDateTime get(TemporalField field) examples
- How to create Date in yyyy-MM-dd format in Java
- Java: Create Temporary Directory and File and Delete when application terminates
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED
- Run Java Code Every Second
More Posts:
- JavaScript: Check if variable is a number - JavaScript
- Java Generics Methods Examples - Java
- How to Make Your Own Cross Platform Custom Notepad Application [Windows/Mac/Linux] - Python
- Clear Screen shortcut macOS Terminal - MacOS
- How to Customize Ribbon in Excel for Mac - MacOS
- Change Android Toast background color - Android
- Java .replace() RegEx alternative in Python Programming - Python
- 16: Find the largest element in a List - 1000+ Python Programs - Python-Programs