Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

New File with a Directory

Thread view: 
Alan - 30 Dec 2007 20:47 GMT
I was wondering why Java behaves this way. . . . Below are a few
lines of code with nearly duplicate methods called for a file and then
a directory.  One method (methodWithoutNew) just assigns a File
variable to the input File object (no "new" operator).  The other
method (methodWithNew) uses the "new" operator to create its file
object with the same name as the input File object.

   When checking whether or not the various files exist at different
points, the only difference in results is that the directory is not
observed to exist in the method that uses the "new" operator.
However, the non-directory file does exist in that same method.

     What is happening here?  Why the difference?

     Also, why don`t I need the "new" operator to create a File
object?

     Both the execution results and the code are shown below.

Thanks, Alan

Results:

----jGRASP exec: java CallWithFile

. . . in methodWithoutNew . . .
File CallWithFile.java exists.
. . . in methodWithNew . . .
File CallWithFile.java exists.
. . . in Main . . .
File CallWithFile.java exists.
. . . in methodWithoutNew . . .
File My Java Code exists.
. . . in methodWithNew . . .
File My Java Code does not exist!!!
. . . in Main . . .
File My Java Code exists.

----jGRASP: operation complete.

Code:

<sscce>
import java.io.*;
import java.util.*;

public class CallWithFile
{

public static void main(String[] args)
{
  File afile = new File("CallWithFile.java");
  methodWithoutNew( afile );
  methodWithNew ( afile );
  System.out.println(" . . . in Main . . . ");
  if ( afile.exists() )
  { System.out.println( afile.getName() + " exists." );}
  else
  { System.out.println( afile.getName() + " does not exist!!!" ); }

  File directory = new File(System.getProperty("user.dir"));
  methodWithoutNew( directory );
  methodWithNew ( directory );
  System.out.println(" . . . in Main . . . ");
  if ( directory.exists() )
  { System.out.println( directory.getName() + " exists." );}
  else
  { System.out.println( directory.getName() + " does not
exist!!!" ); }

}

public static File methodWithoutNew(File infile)
{
File file = infile;
System.out.println(" . . . in methodWithoutNew . . . ");
if ( file.exists() )
{ System.out.println( file.getName() + " exists." );}
else
{ System.out.println( file.getName() + " does not exist!!!" ); }

return file;
}

public static File methodWithNew(File infile)
{
File file = new File(infile.getName());
System.out.println(" . . . in methodWithNew . . . ");
if ( file.exists() )
{ System.out.println( file.getName() + " exists." );}
else
{ System.out.println( file.getName() + " does not exist!!!" ); }

return file;
}

}
<\sscce>
Mark Thornton - 30 Dec 2007 21:41 GMT
>    I was wondering why Java behaves this way. . . . Below are a few
> lines of code with nearly duplicate methods called for a file and then
> a directory.  One method (methodWithoutNew) just assigns a File
> variable to the input File object (no "new" operator).  The other
> method (methodWithNew) uses the "new" operator to create its file
> object with the same name as the input File object.

> { System.out.println( file.getName() + " does not exist!!!" ); }

The reason would be more obvious if you replaced file.getName() with
file in all of the print statements (in this context file is equivalent
to file.toString()).

Mark Thornton
Lew - 30 Dec 2007 21:55 GMT
>>    I was wondering why Java behaves this way. . . . Below are a few
>> lines of code with nearly duplicate methods called for a file and then
[quoted text clipped - 8 lines]
> file in all of the print statements (in this context file is equivalent
> to file.toString()).

Alan: is just the name enough, without the whole path, enough to find the file
to see if it exists?

Signature

Lew

Mark Thornton - 30 Dec 2007 22:16 GMT
>>>    I was wondering why Java behaves this way. . . . Below are a few
>>> lines of code with nearly duplicate methods called for a file and then
[quoted text clipped - 11 lines]
> Alan: is just the name enough, without the whole path, enough to find
> the file to see if it exists?

No. You get the result the OP described unless the current directory
contains a file or directory with the same name as the last part of the
current directory path. Even then you are getting the result wanted for
the wrong reason.
The issue here is confusion between File.getPath() and File.getName().

Mark Thornton
Alan - 30 Dec 2007 23:09 GMT
Mark,
     Thank you.  If I replace

File file = new File(infile.getName());

with

File file = new File(infile.getPath());

in the "methodWithNew" method, then it works.

   So, I guess the bottom line is that I can just set a File object
to the input File object, without needing to use the "new" operator.

   When I do that (no "new"), am I just making the new File object in
the method point to the input File object?  This sounds OK to me.

Thanks, Alan
Lew - 31 Dec 2007 03:01 GMT
> If I replace
>
[quoted text clipped - 11 lines]
>     When I do that (no "new"), am I just making the [not] new File object in
> the method point to the input File object?  This sounds OK to me.

You're making the variable 'file' point to the same object as 'infile'.

This sets the variable 'file' to point to the same File object as 'infile',
yes.  It is, however, not a "new File object", nor is an object ever made to
point to an object.  Objects reference other objects through variables.

File file = infile;

It is the variable 'file' that points to an object, and the variable 'infile'
that points to the same object.  There are two variables, pointing to only one
object.

File file = new File( infile.getPath() );

Now the variable 'file' points to a different File object from 'infile'.
There are two variables, pointing to two different objects.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.