java is emitting build\someFile.txt as you'd expect. To be specific, java emits a byte sequence to standard out which then goes on a trip. That goes into the OS, the OS sends it to whatever's hooked up to your java proces's standard out which at some point presumably takes those bytes, converts it back to a string, and then does something with that. Hopefully in this wild adventure eventually somewhere something turns it into a little picture of some characters and these, eventually, are rendered onto your screen, and from there, into your eyeballs.
Something in that wild adventure is applying backslash escaping. Terminals don't do it, so something else that you did not mention in your question is doing that. Java certainly doesn't do it. Something else is. If you just write this extremely basic:
class Test {
public static void main(String[] args) throws Exception {
Path x = Path.of("foo/bar.txt");
System.out.println(x);
}
}
and run that on a stock windows in a stock-standard windows cmd.exe terminal that prints foo\bar.txt as you'd expect.
Check the chain of apps that are in between the java process and your eyeballs. One of em is messing this up. If you need some help with this process, edit the question and explain in excruciating detail (because you probably don't know what's relevant, this mojibake / terminal stuff can be tricky like that) each and every app or thing that could possibly be in between. Are you running this within an IDE? Which one, and how. Are you running this on windows subsystem for linux? Mention that, how you're doing it, which commands you're typing in, and so on.
Answer from rzwitserloot on Stack Overflowwindows - String of java.nio.file.Path - Stack Overflow
Get java.nio.file.Path object from java.io.File - Stack Overflow
file - How to get the path string from a java.nio.Path? - Stack Overflow
Java nio: How to add extension to an absolute path? - Stack Overflow
Videos
Yes, you can get it from the File object by using File.toPath(). Keep in mind that this is only for Java 7+. Java versions 6 and below do not have it.
From the documentation:
Paths associated with the default
providerare generally interoperable with thejava.io.Fileclass. Paths created by other providers are unlikely to be interoperable with the abstract path names represented byjava.io.File. ThetoPathmethod may be used to obtain a Path from the abstract path name represented by a java.io.File object. The resulting Path can be used to operate on the same file as thejava.io.Fileobject. In addition, thetoFilemethod is useful to construct aFilefrom theStringrepresentation of aPath.
(emphasis mine)
So, for toFile:
Returns a
Fileobject representing this path.
And toPath:
Returns a
java.nio.file.Pathobject constructed from the this abstract path.
Use:
Paths.get(...).normalize().toString()
Another solution woul be:
Paths.get(...).toAbsolutePath().toString()
However, you get strange results: Paths.get("/tmp", "foo").toString() returns /tmp/foo here. What is your filesystem?
To complete the the fge's answer, I would add some info:
normalize()simply removes redundant pieces of strings in your path, like.or..; it does not operate at the OS level or gives you an absolute path from a relative pathtoAbsolutePath()on the contrary gives you what the name says, the absolute path of thePathobject. But...toRealPath()resolves also soft and hard links (yes they exists also on Windows, so win user, you're not immune). So it gives to you, as the name says, the real path.
So what's the best? It depends, but personally I use toRealPath() the 99% of the cases.
As pointed out by Roberto Bonvallet, since toRealPath() throws an exception if the file does not already exist because, for example, you want to create it. In this case I prefer toAbsolutePath().
Source: Official javadoc
Good afternoon, I am currently implementing the native Java File IO functionality within my project in order to write XML data-structures to the disc. This will be used for saving GUI configurations for my portfolio project (A raycasting based game engine)
Everything is working correctly BUT Java defaults to a local absolute-path if the directory of a file is not specified in the handle String. This is what I want, as the engine needs to dynamically store user data in a non-hardcoded directory (obviously) But the default absolute-path is just the root folder of the project; which is not ideal as it is too exposed, will clutter the project folder, and doesn't sit well with my OCD.
How can I find a way around this and have java create the files within a specific resources folder, while not hard-coding this as a directory?