Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split windows path

Tags:

java

regex

Can anybody show me how to split windows path on folders, file and drive ? Do I need regex ?

like image 279
Damir Avatar asked Nov 27 '25 20:11

Damir


2 Answers

No regex needed use java.io.File part of the standard library.

Especially the getName(), getParent() and getParentFile() methods which will be much simpler.

like image 169
mmmmmm Avatar answered Nov 30 '25 10:11

mmmmmm


RegExp? Yes and No - you can use String#split which uses a regular expression even though it often feels like using a normal String:

  String[] parts = "C:\\Program Files\\Application\\config.txt".split("\\\\");

This results in drive (parts[0]), folders (parts[1] and parts[2]) and filename (parts[parts.length-1])

You may have to test if the first segment is a drive name (ends with ":"), the last segment is a file name (file.isDirectory()) and if a segment contains a folder (like no folder in C:\test.txt.

like image 27
Andreas Dolk Avatar answered Nov 30 '25 10:11

Andreas Dolk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!