Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on File Class

 
Vivek Shinde
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks !
A question from JExam
Select the correct statement regarding the following piece of code.
File f = new File("C:\\Large.txt")
a) On execution a file called "Large.txt" will be created on the local harddisk
b)The code fails to compile on a Unix machine because the directory Seperator is not correct
C)A file is not created when the code is executed
d) an excepton is trown at runtime if the file "large.txt" already exits
e) The code fails to compile since this is not a valid constructor for the File class.
I thought the correct answers were b and c . But the answer given is only c. Isn't the file separator '\\' unique to windows ?
I do not have unix on my machine to try it out. Can any one help out ?
Regards
vivek
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vivek,
You're right, the File class uses the filename conventions of the host system; which means the code should cause an error on a Unix system. If you want the code to run on different systems you need to separate the 'path' and 'filename' components and use the 'pathSeparatorChar' which is a field in the File class.
Think it's safe to assume that if a question uses a Window pathname construct the underlying system is also Windows; at least for the purpose of the example.
Hope that helps.
Jane
 
Susan Hoover
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek and Jane,
Yes, the code will most likely cause an error at runtime on Unix, but it will compile just fine. Answer b states that the code fails to compile, and is therefore incorrect.
 
sachin patel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
class Demo
{
public static void main(String a[])
{
String tmp="";
File f = new File("C:\\Large.txt");
System.out.println(f.exists());
}
}

OUTPUT
false

Hello i tried this on unix machine and it compiled fine and when i run the program its output was "false".....
------------------
Sachin,
****************************************************
Learn from others mistakes. Life is too short to make all yourself.
****************************************************
 
Maitham H
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, if we take a close look at what the exam Objectives are.
we can easily eliminate answer b.
Sun is not interested in testing your OS knowledge, so they won't possibly ask questions or want answers to questions that will involve specific Operating Systems architectures.

 
Susan Hoover
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops, I just reread my previous reply and realized it might be unclear. When I said
the code will most likely cause an error at runtime on Unix
I did not mean to imply that it would cause an exception to be thrown, just that the file named "C:\\Large.txt" would not be found, but that would still be some sort of problem occurring at runtime and not at compile time. My second sentence above still stands.
 
Vivek Shinde
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everybody, You were a great help
Vivek
 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
my answer of this question: C is correct.
explaination
for
a) is wrong
when u instance a File class nothing will be created
in harddisck,
b) is wrong
whatever directory seperator for the platform is correct or not
no compiler error, no run time exception, it just sinks. but u can use isexist() to check.
c) is correct,
if u instance a object of File class with a
file name not exist, only problem u will have ,when u
try to read(or write) with the stream which created by this File
object, u will get a IOException.
d) is wrong, but on contrary is right(just c option)
e) is wrong see c) explaintantion.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Susan, Sachin ....
Looks like it will both compile and run fine on another OS ... you just won't find the file.

Jane
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic