import java.io.File;
public class PathResolver {
/**
* Resolves file paths for backward compatibility.
* Handles relative paths and ensures they are resolved relative to the current working directory.
*
* @param filePath The file path to resolve.
* @return The absolute resolved file path.
*/
public static String resolvePath(String filePath) {
File file = new File(filePath);
return file.getAbsolutePath(); // Returns the absolute path
}
public static void main(String[] args) {
// Example Usage
String relativePath = "data/input.txt";
String absolutePath = resolvePath(relativePath);
System.out.println("Relative Path: " + relativePath);
System.out.println("Absolute Path: " + absolutePath);
String anotherRelativePath = "subdir/another_file.csv";
String anotherAbsolutePath = resolvePath(anotherRelativePath);
System.out.println("Relative Path: " + anotherRelativePath);
System.out.println("Absolute Path: " + anotherAbsolutePath);
//Test with current directory
String currentPath = ".\\my_file.txt";
String resolvedCurrentPath = resolvePath(currentPath);
System.out.println("Relative Path: " + currentPath);
System.out.println("Absolute Path: " + resolvedCurrentPath);
}
}
Add your comment