0
File vs Formatter objects
If I got this lesson correctly, you create a File object if the file you want to utilize already exists & you create a Formatter object if youd like to create a file?
1 Réponse
+ 3
not exactly
a File object have a method which create a file if such file doea not exists in the path that was used to open the file .createNewFile()
as stated in the File documentation
https://docs.oracle.com/javase/7/docs/api/java/io/File.html
a file instance is an abstract representation of a file in the system, meaning that to actually working (read/write) with the file you will have to to use some additional objects such as Scanner or FileWriter which are able to work with an instance of a file.
as for the Formatter, that's another class which also able to take a File instance (one of its constructors does exactly that) and work with it
https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
it does allow to write into files, and also set a format when writing.
of course, there are a lot of other ways to write/read files, you just need to pick whichever suits your program needs (for the start, when it might be unclear what are the needs and you only want to read/write, just go with whatever is most convenient)