BAEL-6541-convert-relative-path

This commit is contained in:
tienvn4 2023-06-13 17:02:25 +07:00
parent e91d7301bd
commit b13c7c6ec5
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.convertpaths;
import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
public class RelativePathConverter {
public static String convertToAbsoluteUsePathsClass(String relativePath) {
Path absolutePath = Paths.get(relativePath).toAbsolutePath();
return absolutePath.toString();
}
public static String convertToAbsoluteUseFileClass(String relativePath) {
File file = new File(relativePath);
return file.getAbsolutePath();
}
public static String convertToAbsoluteUseFileSystemsClass(String relativePath) {
Path absolutePath = FileSystems.getDefault().getPath(relativePath).toAbsolutePath();
return absolutePath.toString();
}
public static void main(String[] args) {
String relativePath = "myFolder/myFile.txt";
String absolutePath = convertToAbsoluteUseFileSystemsClass(relativePath);
System.out.println("Absolute Path: " + absolutePath);
}
}