Merge pull request #14232 from vunamtien/BAEL-6541-convert-relative-path
BAEL-6541-convert-relative-path
This commit is contained in:
commit
4b08a40eae
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.convertpaths;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RelativePathConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenRelativePath_whenConvertingToAbsolutePath_thenPrintOutput() {
|
||||
String relativePath = "data/sample.txt";
|
||||
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsolutePath_whenConvertingToAbsolutePath_thenPrintOutput() {
|
||||
String absolutePath = "/var/www/index.html";
|
||||
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(absolutePath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(absolutePath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(absolutePath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyPath_whenConvertingToAbsolutePath_thenPrintOutput() {
|
||||
String emptyPath = "";
|
||||
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(emptyPath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(emptyPath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(emptyPath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParentDirectoryPath_whenConvertingToAbsolutePath_thenPrintOutput() {
|
||||
String relativePath = "../data/sample.txt";
|
||||
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRelativePathContainingDots_whenConvertingToAbsolutePath_thenPrintOutput() {
|
||||
String relativePath = "././data/sample.txt";
|
||||
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath));
|
||||
System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue