• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Some confusing ones part2

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 38: What does the following code do? File f = new File("hello.test");
FileOutputStream out = new FileOutputStream(f);

Select the one right answer.
a.Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it and read from it.
b.Create a new file named "hello.test" if it does not yet exist. The file is not opened.
c.Open a file named "hello.test" so that you can write to it and read from it, but does not create the file if it does not yet exist.
d.Open a file named "hello.test" so that you can write to it but cannot read from it.
e.Create an object that you can now use to create and open the file named "hello.test," and write to and read from the file.
Answer given is a
But we cannot read from FileOutput Stream can any one explain
logic behind it
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope following code will clear your problem
import java.io.*;
class Gd {
public static void main(String args[]) throws Exception{
File f = new File("gdn.txt");
FileOutputStream fo = new FileOutputStream(f);
FileInputStream fi = new FileInputStream(f);
fo.write('x');
System.out.println((char)fi.read());
}
}
 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code is endorsing my viewpoint
you are also reading from file input stream
any other takers for it?

Originally posted by jafarali:
I hope following code will clear your problem
import java.io.*;
class Gd {
public static void main(String args[]) throws Exception{
File f = new File("gdn.txt");
FileOutputStream fo = new FileOutputStream(f);
FileInputStream fi = new FileInputStream(f);
fo.write('x');
System.out.println((char)fi.read());
}
}


 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gaurav,
IMO the answers are not very clear and there are no right answer(s) here.
a.Create a new file named "hello.test" if it does not yet exist.
It also opens the file so you can write to it and read from it.
WRONG.
I agree with the first half, but not the second. You cannot use
FileOutputStream to read data.
b.Create a new file named "hello.test" if it does not yet exist.
The file is not opened.
WRONG.
Again, I agree with the first half, but not the second. You don't
have to issue an explicit open call for FileOutputStream. If the
constructor returns successfully, the file is opened. Any errors
are raised as exceptions when you attempt to write to it.
c.Open a file named "hello.test" so that you can write to it and
read from it,but does not create the file if it does not yet exist.
WRONG.
The file is indeed created if it doesn't exist.
d.Open a file named "hello.test" so that you can write to it but
cannot read from it.
MAY BE.
Then answer is ambiguous. If you have a file, you can almost always
( except when you don't have permissions ) read and write to it.
But within the scope of the question, we are only looking at a
FileOutputStream object. We all know it is only used for writing
and not reading.

e.Create an object that you can now use to create and open the
file named "hello.test," and write to and read from the file.
WRONG.
The FileOutputStream is what is referred to as the "Object" here.
If the stream object is created, it is implied that either an
existing file was opened, or a new file was created. Also, the
stream is already opened.

Ajith
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith strikes again,
 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajith
you have cleared my doubts
and continue to do so
Regards
Gaurav
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic