This week's book giveaway is in the Reactive Progamming forum. We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line! See this thread for details.
I am trying to determine at the start of my program if a file exists. In the File API there is a method, exists() that returns a boolean value if said file exists. In order to access the exists() method, you have to initialize the File object, so effectively you have to create a File to see if it exists.
For example, to see if alpha file exists would you have to...
File alpha = new File("alpha"); S.O.P.(alpha.exists());
...to see if the file exists using this method, when it obviously does because you have to make it to check!
Any suggestions on how to check if a file exists without doing this?
A file wont be created on your disk until you actually write some streams on the File object. So unless you have a specific requirement, you dont really need to check if the file exisits or not ..because it always creates a new file or override the old one in Java.. Am I right on this?
SCJP, SCJD, SCWCD, SCBCD, SCEA
posted 13 years ago
Well I have to write a mapping of newsgroups and articles read onto the file (newsrc).
Originally posted by Greg Walker: Well I have to write a mapping of newsgroups and articles read onto the file (newsrc).
As has been mentioned, createNewFile() will create the file if it doesn't already exist and is an atomic operation. You should look at the classes in java.io for writing to the file. Whether or not a given class will create the file if it doesn't exist, overwrite the file if it exists, or append to it is something you should consult the documentation for informaton on. FileOutputStream and FileWriter both provide constructors that accept the File and a boolean argument specifying whether to append to the file or overwrite it. In my experience with the java.io API most implementations are going to create the file if they can and overwrite the file if it exists unless you specify otherwise.
Once again, this is in regards to using an OutputStream or Writer of some sort. The File class merely represents a File path in the abstract, creating one does not create a new file on disk, passing that abstract representation to another class that will create it is another story. [ September 15, 2006: Message edited by: Ken Blair ]
posted 13 years ago
Okay I'm trying to read in newsgroups from the host newsserver listed in my instance data. The programs compiles but freezes at startup. Here's the code (I guess copy and paste it into an editor to view it easier)