banner



How To Create A Path

4 Answers 4

You can just use the Paths class:

                Path path = Paths.get(textPath);                              

... assuming you want to use the default file system, of course.

answered Jun 4 '13 at 13:45

7

  • Is there a way for this to work with relative path and full path? i.e giving a path relative to where the project or exe is?

    Aug 14 '16 at 17:07

  • @kuhaku: I think you should ask a new question with details of what you're trying to do and what you've tried.

    Aug 15 '16 at 4:00

  • @JonSkeet is Path.get() is platform independent? meaning that Path.get("lib","p2") will be as lib\p2in Windows and lib/p2 in linux

    Jun 22 '17 at 10:00

  • @KasunSiyambalapitiya: Yes, it should be fine like that.

    Jun 22 '17 at 10:00

  • @JonSkeet Paths.get("/opt/path/"); Its returns as "\opt\path\". Can you please provide the solution.

    Sep 27 '17 at 14:50

From the javadocs..http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html

                Path p1 = Paths.get("/tmp/foo");                              

is the same as

                Path p4 = FileSystems.getDefault().getPath("/tmp/foo");  Path p3 = Paths.get(URI.create("file:///Users/joe/FileTest.java"));  Path p5 = Paths.get(System.getProperty("user.home"),"logs", "foo.log");                              

In Windows, creates file C:\joe\logs\foo.log (assuming user home as C:\joe)
In Unix, creates file /u/joe/logs/foo.log (assuming user home as /u/joe)

bummi

26.7k 13 gold badges 60 silver badges 97 bronze badges

answered Sep 11 '13 at 21:57

2

  • I suggest to use File.separarator instead of taking care of the current OS. E.g. "/tmp/foo" is File.separator+"tmp"+File.separator+"foo"

    Sep 12 '13 at 7:20

  • I guess it does not create the actual file, but it creates a Path object. You can use the path object to create the actual file on disk, using Files.createFile(logfilePath).

    Feb 18 '18 at 10:06

If possible I would suggest creating the Path directly from the path elements:

                Path path = Paths.get("C:", "dir1", "dir2", "dir3"); // if needed String textPath = path.toString(); // "C:\\dir1\\dir2\\dir3"                              

answered Sep 27 '15 at 15:26

2

  • is this platform independant?

    Jun 22 '17 at 9:52

  • I am fairly sure that yes, it is.

    Nov 19 '20 at 12:14

Even when the question is regarding Java 7, I think it adds value to know that from Java 11 onward, there is a static method in Path class that allows to do this straight away:

With all the path in one String:

Path.of("/tmp/foo");

With the path broken down in several Strings:

Path.of("/tmp","foo");

Jan Molak

4,130 1 gold badge 35 silver badges 31 bronze badges

answered Jan 3 '19 at 11:36

2

  • Finally they provided this method!

    Jan 4 '19 at 16:15

  • @mat_boy yeah but it's no big deal, really. The method already existed in Java 7, except it was previously called Paths.get.

    Feb 7 '19 at 12:29

Not the answer you're looking for? Browse other questions tagged string path nio java-7 or ask your own question.

How To Create A Path

Source: https://stackoverflow.com/questions/16919501/create-a-path-from-string-in-java7

Posted by: hoglundthatounhould.blogspot.com

0 Response to "How To Create A Path"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel