In my application I have a class called MyObject
. This class must have a member that represents a directory (the object is going to write in this directory).
Should I use a String
or a File
object for this member?
Use a File
object. It's always best to choose an object type that most closely mirrors the usage of the data.
Incidentally, you should never call your object MyObject
. Pick a more meaningful name that describes the purpose of your object.
I don't think it matters much. Probably most clean would be to have a final File field, assuming you need to create a File object anyway.
Edit inspired by comment: What matters more is, if you have a getter for this member, should this getter return File
or String
or perhaps even URL
or URI
? That affects public API of the class, and that should be given some serious thought. And it may be easiest to just not provide getter until you need one (unless it is a library which you need to make "complete"), but instead just have a meaningful toString()
override for debugging purposes etc.
files are terrible to test. try TDD - it will improve your design. i'm not aware of any file abstractions libraries but maybe you should consider looking for one. or at least try to split you business code from file handling code as much as possible
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.