• 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

IO help!

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic