Hello Everybody:
When we create a File instance,just like:
File file = new File("/home/dowson/");
File file = new File("/home/dowson");
What's the difference between the two sentences?
Thanks in advance!
Dowson.
Arne Vajhøj - 06 May 2007 16:40 GMT
> When we create a File instance,just like:
> File file = new File("/home/dowson/");
> File file = new File("/home/dowson");
> What's the difference between the two sentences?
The first is always a dir. The second can be only a dir
and a file.
Often both works fine.
Arne
Paul Tomblin - 06 May 2007 16:40 GMT
In a previous article, Jack Dowson <jckdwsn@aol.com> said:
>Hello Everybody:
>When we create a File instance,just like:
>File file = new File("/home/dowson/");
>File file = new File("/home/dowson");
>What's the difference between the two sentences?
The first one will fail because you didn't give a file name, and the
second one will fail because there is already a directory of that name.

Signature
Paul Tomblin <ptomblin@xcski.com> http://blog.xcski.com/
"As in the thronged and the lighted ways,/so in the dark and the desert
they stand,/Wary and watchful all their days/that their brethren's days
may be long in the land." - Rudyard Kipling, "The Sons of Martha"
Andrew Thompson - 06 May 2007 16:48 GMT
>Hello Everybody:
>When we create a File instance,just like:
>File file = new File("/home/dowson/");
>File file = new File("/home/dowson");
>What's the difference between the two sentences?
The last '/'.
More importantly, neither are formed in an x-plat
manner. The best way to do it is.
String sep = System.getProperty("file.separator");
File home = new File(sep + "home");
File target = new File( home, "dowson" );
<sscce>
import java.io.File;
import java.io.IOException;
class FileOperations {
public static void printFileStatus(File f) {
String status = ( f.isDirectory() ?
"DIR" : ( f.isFile() ?
"FILE" : "NONE"));
System.out.println(
"exists: " + f.exists() +
" \tpath: " + f +
"\nDir/File: " + status );
}
public static void main(String args[])
throws IOException {
String sep =
System.getProperty("file.separator");
File homey = new File(sep + "homey");
File boy = new File(homey, "boy" + sep);
printFileStatus( boy );
System.out.println(
"\tcreate the directories");
boy.mkdirs();
printFileStatus( boy );
System.out.println(
"\tdelete the child directory");
boy.delete();
System.out.println(
"\tcreate the file");
boy.createNewFile();
printFileStatus( boy );
System.out.println(
"\tdelete the file & parent directory");
boy.delete();
homey.delete();
printFileStatus( boy );
printFileStatus( homey );
}
}
</sscce>
HTH

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Matej Cepl - 06 May 2007 17:00 GMT
> The last '/'.
>
[quoted text clipped - 5 lines]
> File home = new File(sep + "home");
> File target = new File( home, "dowson" );
Sorry, this doesn't make much sense to me -- /home construct is
platform dependent in itself and your solution won't save the day
on Windows, where /home is something like C:\Documents and
Settings (or something different, I haven't touched Windows for
so long, that I am not sure about that; moreover, IIRC this is
different in different versions of Windows). You would be much
better with using
File home = System.getProperty("user.home");
(which is unfortunately only $HOME for the current user, but
there should be some way, how to do it for any user, just not
sure how).
Best,
Matěj
Andrew Thompson - 06 May 2007 17:31 GMT
> Andrew T wrote:
(better..)
>> File home = new File(sep + "home");
>> File target = new File( home, "dowson" );
[quoted text clipped - 3 lines]
>
>File home = System.getProperty("user.home");
Oh yeah. Good point.
Jack Dowson - 06 May 2007 19:22 GMT
Firstly,Thank you very much!
> String sep =
> System.getProperty("file.separator");
I have a problem:"file.separator".
Java is case sensitive,but I can compile without any error when "f" in
"file.separator" is lower case.
Why?
Dowson.
Andrew Thompson - 06 May 2007 19:48 GMT
>Firstly,Thank you very much!
>> String sep =
>> System.getProperty("file.separator");
// see what the value of this property is..
System.out.println(sep);
sep =
System.getProperty("File.separator");
// see what the value of this property is..
System.out.println(sep);
>I have a problem:"file.separator".
>Java is case sensitive,but I can compile without any error when "f" in
>"file.separator" is lower case.
>Why?
Don't be afraid to experiment, or read the JavaDocs.

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Jack Dowson - 07 May 2007 04:34 GMT
Firstly,Thank you very much!
I've tried:
import java.io.*;
public class Exp{
public static void main(String[] args){
String sep1 = System.getProperty("file.separator");
System.out.println(sep1);
String sep2 = System.getProperty("File.separator");
System.out.println(sep2);
String sep3 = System.getProperty(File.separator);
System.out.println(sep3);
}
}
The result is:
\
null
null
Why null?
Dowson.
Andrew Thompson - 07 May 2007 06:28 GMT
...
>Why null?
(from earlier)
Don't be afraid to ...read the JavaDocs.
Do you have the JavaDocs downloaded for local browsing?
They are invaluable for Java programmers. I might refer to
them 'every other hour' when doing programming that is
even slightly interesting.

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Jack Dowson - 07 May 2007 11:02 GMT
> Do you have the JavaDocs downloaded for local browsing?
> They are invaluable for Java programmers. I might refer to
> them 'every other hour' when doing programming that is
> even slightly interesting.
Thank you,Thompson!
I know what you mean!And I also know your advise will do good to me!
But I just don't know where to find the details related with this
problem in SDK documentation which includes such a lot of contents!
Dowson!
Andrew Thompson - 07 May 2007 13:08 GMT
>> Do you have the JavaDocs downloaded for local browsing?
...
>But I just don't know where to find the details related with this
>problem in SDK documentation which includes such a lot of contents!
When in doubt, go to the main page,
<http://java.sun.com/javase/6/docs/api/index.html>
..find the class mentioned (System) in the left lower column,
<http://java.sun.com/javase/6/docs/api/java/lang/System.html>
..then check the method (getProperty(String)).
<http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperty(java.lan
g.String
)>
Besides all your conversational replies, please answer
my question, above. That question, again, is..
"Do you have the JavaDocs downloaded for local browsing?"

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Jack Dowson - 07 May 2007 14:52 GMT
> "Do you have the JavaDocs downloaded for local browsing?"
Of course, I do!
Lars Enderin - 06 May 2007 19:51 GMT
Jack Dowson skrev:
> Firstly,Thank you very much!
>> String sep =
[quoted text clipped - 4 lines]
> "file.separator" is lower case.
> Why?
Names of variables, methods, classes, etc are case sensitive, but
"file.separator" is just a String literal used as a parameter, which
happens to spell "file" in lower case.
Karl Uppiano - 07 May 2007 05:06 GMT
> Firstly,Thank you very much!
>> String sep =
[quoted text clipped - 4 lines]
> "file.separator" is lower case.
> Why?
Because "file.separator" is just a property name - it is a run-time concept
and the compiler has no opinions about it.
System.getProperty("File.separator") will probably return null, because it
is unlikely that any property exists in the system properties with that
name.
Richard Senior - 06 May 2007 16:51 GMT
Assuming import java.io.File:
> File file = new File("/home/dowson/");
> File file = new File("/home/dowson");
> What's the difference between the two sentences?
Well, have you tried it?
In practical terms, it makes no difference. If you use
file.createNewFile() in either case, you end up with a file. If you use
file.mkdir(), assuming /home exists, you end up with a directory.

Signature
Regards,
Richard