Java os path operations, joins, and moving up levels -


i have 2 related questions regarding files, , file class in java.

i gather best practice way build path - , have os agnostic - this:

file file = new file("dir" + file.separator + "filename.ext"); 

my first question is, "is there java equivalent of python os.path.join function built java?" i.e. there function can this:

string path = some_func("arbitrary", "number", "of", "subdirs", "filename.ext"); 

i suspect if such thing exists, may need pass array of strings function, rather arbitrary number of args, above ideal.

but regardless of answer question above, second question is, "is there built in way move level when specifying path?"

i.e. correct way, this:

string rel_path = ".." + file.separator + "filename.ext"; 

or there this:

string rel_path = file.level_up + file.separator + "filename.ext"; 

cheers all!

i gather best practice way build path - , have os agnostic - this:

file file = new file("dir" + file.separator + "filename.ext"); 

or (see api documentation on constructors of java.io.file):

file file = new file("dir", "filename.ext"); 

note takes 2 parameters - name of parent directory , filename (not arbitrary list of subdirectories).

you're looking java.nio.file.paths.get():

path path = paths.get("arbitrary", "number", "of", "subdirs", "filename.ext"); 

note gives path rather file, if need file call tofile() on path.

note: new stuff in java 7.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -