File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IO help!

 
Rowan Chattaway
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Matt Ghiold
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic