https://kotlinlang.org logo
Title
v

ValV

01/09/2019, 8:20 PM
What is the better way to handle system path (
/path/to/file
), than store it as a
String
? Better way to split it later by directories (
path
,
to
,
file
)?
h

hudsonb

01/09/2019, 8:23 PM
If you are talking strictly JVM, then `Path`: https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html
d

Dominaezzz

01/09/2019, 8:24 PM
If you have the time to implement the members, maybe a
inline class Path(private val value: String)
on multi-platform.
v

ValV

01/09/2019, 8:30 PM
So the actual path will be stored as a
String
inside a
Path
class, won't it?
d

Dominaezzz

01/09/2019, 8:31 PM
Yes, on the other platforms. On JVM it could be the
java.nio.file.Path
.
v

ValV

01/09/2019, 8:32 PM
Also I need the path to be serialized, so, perhaps, I should not use Java's
Path
?
d

Dominaezzz

01/09/2019, 8:34 PM
Oh, then definitely stick to
String
.
v

ValV

01/09/2019, 8:35 PM
I was afraid of that
So another question: how to split it effectively? Regex or there are some better methods in Kotlin?
d

Dominaezzz

01/09/2019, 8:38 PM
"/path/to/file".split('/')
should do the trick.
You may have to filter empty strings.
v

ValV

01/09/2019, 8:39 PM
Great, thanks 🙂
f

fred.deschenes

01/09/2019, 8:45 PM
you could still use
Path
but serialize it as an absolute path string, as working with paths is a PITA
j

joelpedraza

01/09/2019, 8:59 PM
"/path/to/file".split('/')
Beware the platform.
/
might not be the path seperator
☝️ 1
f

fred.deschenes

01/09/2019, 9:02 PM
seriously, paths are as much of a pandora's box as timezones and string encodings
1
v

ValV

01/09/2019, 9:04 PM
Ouch, so I need
Path
to handle cross-platform? But it seems tricky to serialize
But, perhaps, I should store in a database that path with
/
, but convert to
Path
when I perform file IO operations?