Is there a method to combine paths?

Asked 1 years ago, Updated 1 years ago, 77 views

Is there a method like System.IO.Path.Combine() in Java if you look at C# or dot net? Or is it possible with the code?

java path

2022-09-22 11:35

1 Answers

If you use Java 7 or 8, you can use java.nio.file.Path. Path.resolve is used to merge one path with another. Paths classes are also useful.

Path path = Paths.get("foo", "bar", "baz.txt"); You can use it like this.

If you're using an earlier version of Java7, you can do this at java.io.File.

File baseDirectory = new File("foo");
File subDirectory = new File(baseDirectory, "bar");
File fileInDirectory = new File(subDirectory, "baz.txt");

If you want to receive it as a string, use getPath()

public static String combine(String path1, String path2)
{
    File file1 = new File(path1);
    File file2 = new File(file1, path2);
    return file2.getPath();
}

You can do it like this.


2022-09-22 11:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.