There are many places where that will not work. All system resources do not have a file name. For example if your class and text file were inside a jar file.
It is better to work with system resources by getting a stream to that file. If you really need file operations, I would suggest writing it to a temporary file. Most of the time a stream suffices.