Query:difference between with "/" and without "/"?

J

Jack Dowson

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

Guest

Jack said:
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
 
P

Paul Tomblin

In a previous article 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.
 
A

Andrew Thompson

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

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1
 
R

Richard Senior

Jack Dowson wrote:

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

Matej Cepl

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" );

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
 
A

Andrew Thompson

Matej said:
Andrew T wrote: (better..)

Sorry, this doesn't make much sense to me --
... You would be much better with using

File home = System.getProperty("user.home");

Oh yeah. Good point.
 
J

Jack Dowson

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

Andrew Thompson

Jack said:
Firstly,Thank you very much!
// 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.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1
 
L

Lars Enderin

Jack Dowson skrev:
Firstly,Thank you very much!

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

Jack Dowson

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

Karl Uppiano

Jack Dowson said:
Firstly,Thank you very much!

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?

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

Andrew Thompson

Jack Dowson wrote:
...
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.
 
J

Jack Dowson

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!
 
A

Andrew Thompson

Jack said:
...
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.lang.String
)>

Besides all your conversational replies, please answer
my question, above. That question, again, is..
"Do you have the JavaDocs downloaded for local browsing?"

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top