How do I use File.exists()?

Discussion in 'Java' started by Danger_Duck, Aug 15, 2008.

  1. Danger_Duck

    Danger_Duck Guest

    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
    Danger_Duck, Aug 15, 2008
    #1
    1. Advertising

  2. Danger_Duck

    Dave Miller Guest

    Danger_Duck wrote:
    > 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>

    --
    Dave Miller
    Java Web Hosting
    http://www.cheap-jsp-hosting.com/
    Dave Miller, Aug 15, 2008
    #2
    1. Advertising

  3. Danger_Duck

    Tom Anderson Guest

    On Fri, 15 Aug 2008, Danger_Duck wrote:

    > 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

    --
    Why do we do it? - Exactly!
    Tom Anderson, Aug 15, 2008
    #3
  4. Danger_Duck

    Roedy Green Guest

    On Fri, 15 Aug 2008 06:05:04 -0700 (PDT), Danger_Duck
    <> wrote, quoted or indirectly quoted someone
    who said :

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


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

    see http://mindprod.com/jgloss/file.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 15, 2008
    #4
  5. Danger_Duck

    Danger_Duck Guest

    On Aug 15, 9:26 am, Tom Anderson <> wrote:
    > On Fri, 15 Aug 2008, Danger_Duck wrote:
    > > 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
    >
    > --
    > Why do we do it? - Exactly!


    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.
    Danger_Duck, Aug 15, 2008
    #5
  6. Danger_Duck

    Roedy Green Guest

    On Fri, 15 Aug 2008 11:29:57 -0700 (PDT), Danger_Duck
    <> wrote, quoted or indirectly quoted someone
    who said :

    >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.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 28, 2008
    #6
  7. Danger_Duck

    magloca Guest

    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.
    magloca, Aug 29, 2008
    #7
  8. Danger_Duck

    EJP Guest

    magloca wrote:
    >> Hmm. File.createNewFile() must be quite an embarrassing misnomer,
    >> then.

    >
    > 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.'
    EJP, Aug 31, 2008
    #8
  9. Danger_Duck

    Roedy Green Guest

    On Thu, 28 Aug 2008 15:44:33 -0400, Harold Shand
    <> wrote, quoted or indirectly quoted someone who
    said :

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


    To be more precise, new File does not create a file.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 31, 2008
    #9
  10. Danger_Duck

    magloca Guest

    EJP @ Sunday 31 August 2008 11:30:

    > magloca wrote:
    >>> Hmm. File.createNewFile() must be quite an embarrassing misnomer,
    >>> then.

    >>
    >> 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.'


    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.
    magloca, Aug 31, 2008
    #10
  11. EJP wrote:
    > magloca wrote:
    >>> Hmm. File.createNewFile() must be quite an embarrassing misnomer,
    >>> then.

    >>
    >> 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.'


    Looks pretty explict to me.
    Mike Schilling, Aug 31, 2008
    #11
  12. On 31/08/2008 18:32, Mike Schilling allegedly wrote:
    > EJP wrote:
    >> magloca wrote:
    >>>> Hmm. File.createNewFile() must be quite an embarrassing misnomer,
    >>>> then.
    >>> 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.'

    >
    > Looks pretty explicit to me.
    >


    I think that was his point. It's not implicit.

    --
    DF.
    Daniele Futtorovic, Sep 1, 2008
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad Crowder
    Replies:
    7
    Views:
    21,477
    hemant.yadav
    Oct 24, 2009
  2. EdwardH
    Replies:
    9
    Views:
    522
    Dimitri Maziuk
    Sep 22, 2005
  3. Totan
    Replies:
    0
    Views:
    898
    Totan
    Apr 17, 2006
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,089
  5. mynews
    Replies:
    4
    Views:
    201
    RedGrittyBrick
    Nov 26, 2008
Loading...

Share This Page