Java – how do I use relative paths instead of absolute paths?
•
Java
So I have a strange question about Java
I'm reading and writing files, so the path is very important to me I want all files to be written and read from relative paths (that is, in the folders of the rest of my classes and java files)
I write this file:
FileWriter fw = new FileWriter(outfile,true); fw.write(data); fw.close();
Outfile is like 'out Txt '(that is, the name of the file we want to output into)
The problem is that the file was created in / home / me /, not the path to my other files
Did I do something wrong? Or how can I store files in relevant directories?
Solution
FileWriter fw = new FileWriter("./" + outfile,true);
FileWriter fw = new FileWriter("./" + outfile,true);
or
FileWriter fw = new FileWriter("~/" + outfile,true);
I'll try
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码