How do I use File.exists()?

D

Danger_Duck

Almost a silly question, but I want to make sure I have it right:

Java.io.File:

Say I want to know if C:\folder\file.txt exists.

Do I just do this:

File file = new File(C:\folder\file.txt);

if (file.exists()) {
//confirm existence
System.out.print("it's there dude \n");
}

Does the above work for confirming whether or not the file is there?
If so, I guess the File class doesn't ACTUALLY create new files?
Because without reading any documentation I would think the first line
actually made a file there and therefore (of course!) it will
exist...NOT

I ask because the javadoc isn't too clear-does the file instance
actually mean a new file?
Thanks to whoever can enlighten me on what this is actually doing.

File

public File(String pathname)

Creates a new File instance by converting the given pathname
string into an abstract pathname. If the given string is the empty
string, then the result is the empty abstract pathname.

Parameters:
pathname - A pathname string
Throws:
NullPointerException - If the pathname argument is null
 
D

Dave Miller

Danger_Duck said:
Almost a silly question, but I want to make sure I have it right:

Java.io.File:

Say I want to know if C:\folder\file.txt exists.

Do I just do this:

File file = new File(C:\folder\file.txt);

if (file.exists()) {
//confirm existence
System.out.print("it's there dude \n");
}
Yes

Does the above work for confirming whether or not the file is there?
If so, I guess the File class doesn't ACTUALLY create new files?
Because without reading any documentation I would think the first line
actually made a file there and therefore (of course!) it will
exist...NOT


File creates a file instance use FileOutputStream to write it to disk.

<snip>
 
T

Tom Anderson

Almost a silly question, but I want to make sure I have it right:

Java.io.File:

Say I want to know if C:\folder\file.txt exists.

Do I just do this:

File file = new File(C:\folder\file.txt);

if (file.exists()) {
//confirm existence
System.out.print("it's there dude \n");
}

Does the above work for confirming whether or not the file is there?
If so, I guess the File class doesn't ACTUALLY create new files?

The thing to understand is that File isn't actually a file. It's a
filename. The class is badly named, sadly. Once you've realised that, i
think everything else is clear.
Because without reading any documentation I would think the first line
actually made a file there and therefore (of course!) it will
exist...NOT

That's definitely the logical thing to think - and sadly, it isn't true!
But once you think of it as creating a filename rather than an actual
file, it makes sense.

tom
 
D

Danger_Duck

The thing to understand is that File isn't actually a file. It's a
filename. The class is badly named, sadly. Once you've realised that, i
think everything else is clear.


That's definitely the logical thing to think - and sadly, it isn't true!
But once you think of it as creating a filename rather than an actual
file, it makes sense.

tom

Thanks-that clarifies things. File=filepath

And Roedy-yes, you're right, that's what I meant to put :)
I have the string stored separately in my program anyway so I guess I
wasn't paying that much attention when I was asking the question.
 
R

Roedy Green

Thanks-that clarifies things. File=filepath

The File class is primarily for composing and taking apart filenames.
It has a few other methods like delete and exists that actually do
something to files. But it most definitely does not create or open a
file.
 
M

magloca

Harold Shand @ Thursday 28 August 2008 21:44:
Roedy Green wrote: [snip]
The File class is primarily for composing and taking apart filenames.
It has a few other methods like delete and exists that actually do
something to files. But it most definitely does not create or open a
file.

Hmm. File.createNewFile() must be quite an embarrassing misnomer,
then.

But it most definitely does not *implicitly* create or open a file.

m.
 
E

EJP

magloca said:
But it most definitely does not *implicitly* create or open a file.

Bzzt. Of course it does. RTFM. Or try it and see.

'Atomically creates a new, empty file named by this abstract pathname if
and only if a file with this name does not yet exist.'
 
M

magloca

EJP @ Sunday 31 August 2008 11:30:
Bzzt. Of course it does. RTFM. Or try it and see.

'Atomically creates a new, empty file named by this abstract pathname
if and only if a file with this name does not yet exist.'

Maybe I snipped too much of the preceding discussion leading up to my
comment. The "it" in my statement refers to the File class, not the
createNewFile() method. Instantiating a File object doesn't implicitly
create the file in the filesystem; invoking the createNewFile() does,
explicitly.

Sorry about that.

m.
 
M

Mike Schilling

EJP said:
Bzzt. Of course it does. RTFM. Or try it and see.

'Atomically creates a new, empty file named by this abstract
pathname
if and only if a file with this name does not yet exist.'

Looks pretty explict to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top