permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes File class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "File class" Watch "File class" New topic
Author

File class

Wai Iu
Ranch Hand

Joined: Apr 04, 2000
Posts: 39
Under the condition: File f = new File("c:\\large.txt"), is
the following statement true or false?
The code fails to compile on a UNIX machine, because the directory separator is not correct.
I think the above statement is true because the new file object
f seems to be created in Window system. If the same condition-
File f = new File("c:\\large.txt") would be used in UNIX, it
would fail to compile. Am I right? Please help me.
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1441
Its a little trickier than that. The default separator is a /. Underneath the covers you could have a path with \'s in it and the file class will make them /'s. You can set what you want the separator to be. Had to worry about this because our software could be run on NT or UNIX and the users needed to enter filepaths.
Unfortunately my book which describes all this and my Unix machine are both at work and I can't get at them until tomorrow.
If someone doesn't provide a definitive answer by tomorrow I'll check this out.


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
The point to note here is ,

File f = new File("c:\\large.txt");
Is the following statement true or false?
The code fails to compile on a UNIX machine, because the directory separator is not correct.

The File class constructor changes the argument string as an abstract path name. Other than that it doesn't check anything at all. It is 'RandomAccessFile' class which checks if the file exists or not/ok or not etc. during construction itself. So just construction of a File class WILL NOT give any compile error ,whatever path-separator/file-separator you give on any platform. But you have to give the escape chars correctly. (i.e) \\ (2 backslashes). During runtime when the File object is used to create other input streams only, the problem come into picture. A runtime IOException will be thrown ,if the file doesn't exist.
Also if you see the File class constructors in the Java API there is no specification of any Exceptions thrown. But RandomAccess File throws IOException during construction itself.
Also the when you hard-code the pathseparator/fileseparator then that code is not 100% pure java. (i.e) not 100% platform independent. You have to code in a generalized manner. Use the File.separator /System.getProperty("file.separator") methods to get the platform dependent values and then substutute them while coining the file/pathNames and then use it in the construction of File/RandomAccessFile. Simillarly you have to take care of other System related properties from System.getProperty() and use them in appropriate situations.
So the answer for the above qstn is false. The code will compile in both above mentioned platforms.
regds
maha anna
[This message has been edited by maha anna (edited April 11, 2000).]
Wai Iu
Ranch Hand

Joined: Apr 04, 2000
Posts: 39
Thank! Wetherbie and anna! I have another double about this
question. File f = new File("c:\\large.txt") will create a file
object f, which is a refernce to a file-large.txt in local drive
C. However, If I use UNIX workstation, there is no local drive C
there. Does the File f = new File("c:\\large.txt") still work?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
No. I don't think so. Because, UNIX platforms have a single root directory, namely "/". Any 'absolute' path will start with "/" in UNIX and any 'absolute' path in WIN will start with "a:" or "c:" or "d:" etc. ALso note that the 'isAbsolute()' method in File class will act accordingly. This method will return true in WIN, only if the 'path' which is (parent+child) in new File(String parent, String child) starts with c:/a:/d: etc. Simillarly, the same .isAbsolute() will return true in UNIX only if the path(parent+child) starts as "/".
So my suggestion is don't hard-code the filename-separator /path-separator/ the root. Instead use File.separator/File.pathSeparator/ the static method File.listRoots() to obtain the platform specific roots, while forming the arguments to the File class in order to work perfectly in platform independent manner.
regds
maha anna
[This message has been edited by maha anna (edited April 12, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
If I remember correctly creating a file object dose not actually create the file or check if it exists it mearly creates a refrence so it should compile fine.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: File class
 
Similar Threads
unreachable statement
Jxam sample question
Question about unreachable code
Question About "instanceof" operator
Question on File Class