Making computer independent file path
How can I add some files into my project that will have file path independent from computer on which program is executed. I mean, I am using file "Names.txt" located on desktop and in order to read/write in it I have to use file-path something like "C:\\Users\\XYZ\\Desktop\\Names.txt" where XYZ is user-name dependent.
(I have created a new package called Files and saved "Names.txt" in Files Is there any "short" file path now something like "src\\Files\\Names.txt or so?)
Re: Making computer independent file path
You could use a relative path from the 'current' directory. Have you tried: Files/Names.txt?
Write a short test program to use various paths to see where output is written to.
Re: Making computer independent file path
You can read/write a file relative to your project using the 'getClass().getResource()' function (see Class ). For example, if your class is named File/MyClass, then
Code java:
MyClass cl = new MyClass();
URL file = cl.getClass().getResource();
File myFile = new File(file.toURI());
the URL gives the path to the file. This should allow you to read/write to a file in the File package.
Re: Making computer independent file path
It helped Norm I created one output file with name "Dndndndn.txt" and found where it was saved.