Re: Query:difference between with "/" and without "/"?
Jack Dowson wrote:
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