1. import java.io.File;
  2. public class PathResolver {
  3. /**
  4. * Resolves file paths for backward compatibility.
  5. * Handles relative paths and ensures they are resolved relative to the current working directory.
  6. *
  7. * @param filePath The file path to resolve.
  8. * @return The absolute resolved file path.
  9. */
  10. public static String resolvePath(String filePath) {
  11. File file = new File(filePath);
  12. return file.getAbsolutePath(); // Returns the absolute path
  13. }
  14. public static void main(String[] args) {
  15. // Example Usage
  16. String relativePath = "data/input.txt";
  17. String absolutePath = resolvePath(relativePath);
  18. System.out.println("Relative Path: " + relativePath);
  19. System.out.println("Absolute Path: " + absolutePath);
  20. String anotherRelativePath = "subdir/another_file.csv";
  21. String anotherAbsolutePath = resolvePath(anotherRelativePath);
  22. System.out.println("Relative Path: " + anotherRelativePath);
  23. System.out.println("Absolute Path: " + anotherAbsolutePath);
  24. //Test with current directory
  25. String currentPath = ".\\my_file.txt";
  26. String resolvedCurrentPath = resolvePath(currentPath);
  27. System.out.println("Relative Path: " + currentPath);
  28. System.out.println("Absolute Path: " + resolvedCurrentPath);
  29. }
  30. }

Add your comment