Path Name

I read this on the Java docs:



“An optional system-dependent prefix string, such as a disk-drive specifier, “/” for the UNIX root directory, or “” for a Microsoft Windows UNC pathname,”



This is about the path separator for when you’re creating a file. Does it mean that I must first check if the game is running on Linux, and change it to “/” if it is, else make it “”?

That depends on what you are trying to do…

I’m trying to create and load a file, and giving it a path to where it should be created.

UNC paths in windows look lite [xml]\serversharedirectory[/xml]

windows paths looks like [xml]C:directory[/xml]



The strangeness happen because java tries to make file systems behave similar and assumes the “file system” doesn’t start with C: - it is rooted at "” and the “C:” isn’t part of that, it is a “prefix”.



Also, java takes care of the slash - backslash conversion



So, if you are creating a relative name it doesn’t matter (relative to for example users home directory or some directory they specified using a JFileChooser). You should be able to do something like new File(parentFileFromChooser, “mynewfile.dat”);



If you plan on hardcoding absolute pathnames in the application then plan again, only sorrow comes from that :slight_smile:



edit, I can’t write slash or backslashes, the forum eats them :slight_smile: UNC paths names are backslashbackslash server backslash share backslash directory

I’m using this to create a folder:



[java]File f = new File(System.getProperty(“user.dir”) + “/Dungeons”);[/java]



And this to create the xml file inside it:



[java]StreamResult result = new StreamResult(new File(“Dungeons/Plan.xml”));[/java]



Is this a ‘relative name’?

Look at the java File/paths documentation, it explains how it all works in detail.

@memonick said:
I'm using this to create a folder:

[java]File f = new File(System.getProperty("user.dir") + "/Dungeons");[/java]


You could also do:
[java]File f = new File( new File(System.getProperty("user.dir")), "Dungeons");[/java]

...and just not worry about the separator.

Your second example is a relative path from the current working directory... which may be completely unrelated to the user.dir.
1 Like

@pspeed, So with it, I don’t have to worry about different Operating Systems?



Also, do I have to change something with these?



Writing a new XML file:

[java]StreamResult result = new StreamResult(new File(“Dungeons/Plan.xml”));[/java]



Read an existing XML file:



[java]File fXmlFile = new File(“Dungeons/Plan.xml”);[/java]

@memonick said:
@pspeed, So with it, I don't have to worry about different Operating Systems?

Also, do I have to change something with these?

Writing a new XML file:
[java]StreamResult result = new StreamResult(new File("Dungeons/Plan.xml"));[/java]

Read an existing XML file:

[java]File fXmlFile = new File("Dungeons/Plan.xml");[/java]


When java.io.File is adding the separators for you then of course it is doing the right thing with respect to OS.

Your last two are dealing with files relative to the current working directory... ie: where you ran the game. If that's what you wanted them to do then that's fine.

And Java is setup so that forward slash '/' works on any platform anyway... but that has nothing to do with the fact that those are paths relative to the current working directory.
1 Like

Thanks pspeed. If it’s not much to ask, can you please explain this last part?


but that has nothing to do with the fact that those are paths relative to the current working directory.


On what were you talking, please?
@memonick said:On what were you talking, please?


The working directory that the application was launched from
1 Like

Thanks :slight_smile: