This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IO help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IO help!" Watch "IO help!" New topic
Author

IO help!

Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Hello! can anyone help me out with this?
What writes 'ratty' to the end of the file 'ratty.txt'??
a)OutputStream out = new FileOutputStream("ratty.txt");
Out.writeBytes("ratty");
b)OutputStream os = new FileOutputStream("ratty.txt", true);
DataOutputStream out = new DataOutputStream(os);
out.writeBytes("ratty");
c)OutputStream os = new FileOutputStream("ratty.txt");
DataOutputStream out = new DataOutputStream(os);
out.writeBytes("ratty");
d)OutputStream os = new OutputStream("ratty.txt", true);
DataOutputStream out = new DataOutputStream(os);
out.writeBytes("ratty");
Can someone go through each option and explain it too me please?
ps - Sorry for posting loads today, but I've entered into 'death' revision mode.........
Rowan.


The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Here's my shot at it
a)OutputStream out = new FileOutputStream("ratty.txt");
Out.writeBytes("ratty");
This will work, because it will create a file
called ratty.txt, overwritting the current file, so you wont be appending to it and at the end of the file because you just created it will have ratty.
b)OutputStream os = new FileOutputStream("ratty.txt", true);
DataOutputStream out = new DataOutputStream(os);
out.writeBytes("ratty");
This is fine as well, because it specifies the append argument in the FileOutputStream object.
c)OutputStream os = new FileOutputStream("ratty.txt");
DataOutputStream out = new DataOutputStream(os);
out.writeBytes("ratty");
Same as answer A.
d)OutputStream os = new OutputStream("ratty.txt", true);
DataOutputStream out = new DataOutputStream(os);
out.writeBytes("ratty");
Won't compile, you cant specify a boolean to a OutputStream constructor
Hope this helps,
-Matt


-Matt<br /> SCJP2<br /> SCJD
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
okay, I've just wrote a few programs and consulted the API and:
a)this won't compile, complains at the writeBytes method.
b) This is the correct answer, the boolean append means that the test 'ratty' will be written at the end i.e. appending to the file rat.txt
c)this will compile and run, however without the boolean append, it will write over previous contents, and will just produce a file called rat.txt with the string ratty in it.....while the question asks to write ratty to the end of the file.
d)Won't compile, as OutputStream is abstract and cannot be instantiated.
I hope this is all correct!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: IO help!
 
Similar Threads
how to send console output to a html page
Problem in Networking concept
Unable to update my data.. why is that so..?
Not able to send a mail using JAVA code
DataOutputStream along with some Garbage