Can anybody show me how to split windows path on folders, file and drive ? Do I need regex ?
No regex needed use java.io.File part of the standard library.
Especially the getName(), getParent() and getParentFile() methods which will be much simpler.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With