-2

how to create a path in java without define the folder location? for example:

String directory = "/works/abc/"
File fileDirectory = new File(directory );

This directory may be can save at local c, or may be can save at local D. When user change location disk, so that java can auto find that folder.

Thanks

Sharon Wong
  • 109
  • 1
  • 2
  • 14
  • 3
    Are you talking about a [relative path](http://stackoverflow.com/questions/21060992/how-does-java-resolve-a-relative-path-in-new-file)? – PM 77-1 Dec 22 '16 at 14:03

1 Answers1

2

Using the nio api:

Path directory = Paths.get("/works/abc/");
Path drive = Paths.get("C:/"); // e.g.
Path result = drive.resolve(directory); // result: C:\works\abc

If you really need a file, you can use result.toFile()

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93