New File with a Directory

A

Alan

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

Mark Thornton

Alan said:
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
 
L

Lew

Mark said:
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()).

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

Mark Thornton

Lew said:
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
 
A

Alan

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
 
L

Lew

Alan said:
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 [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.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top