Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file instance

 
Mamta Jha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone,
1> In the foll. ques
To create a file,you can use an instance of class
a>File
b>randomaccessfile
c>FileOutputStream
d>any of these //ans
e>b and c
but acc. to me it should be a>File as we use an instance of File class to create a file like,
File f=new File("file name");
FileOutputStream fos=new FileOutputStream(f);
RandomAccessFile ras=new RandomAccessFile(f);
2> Can an applet be added to a container?
I think it can't be as it itself is a container
pl.anybody clear my doubt
thanks in advance,
Mamta
 
Bin Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)I think ans should be e) since File class can only be use to manipulate the file system,not to create files.
2)Of course applet can be added a container since container can include containers.
Anyone can correct me?
 
zulfiqar raza
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For question 1, its (d), because if you use
RandomAccessFile with mode "rw" and the file does not exist, a new empty file is created. Similarly for FileOutputStream, if the file does not exist, it is created. So the answer is (d) any of these.
For question 2, The answer is yes, as containers can contain containers.
thanks,
Zulfiqar Raza.

Originally posted by Mamta Jha:
hi everyone,
1> In the foll. ques
To create a file,you can use an instance of class
a>File
b>randomaccessfile
c>FileOutputStream
d>any of these //ans
e>b and c
but acc. to me it should be a>File as we use an instance of File class to create a file like,
File f=new File("file name");
FileOutputStream fos=new FileOutputStream(f);
RandomAccessFile ras=new RandomAccessFile(f);
2> Can an applet be added to a container?
I think it can't be as it itself is a container
pl.anybody clear my doubt
thanks in advance,
Mamta

 
Udayan Naik
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am of the opinion that answer d) is the correct choice here.
1)You can use an instance of the File class to create a new file if the file passed to the File class constructor does not exist , by means of createNewFile() method.
2)FileOutputStream will also create the file passed to it and start writing to it if the specified file does not exist.
3)For RandomAccessFile , if the mode is 'rw' then the file will be created on the underlying system.
------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any of the answers given so far may be correct, but if you write some code to use these classes, you will know for certain what really happens. Javac is a great teacher.
Marcus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic