This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

The FileOutputStream  RSS feed

 
Richard Strutt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm taking my SCJP exam tomorrow afternoon and was working through some practice exams. I came accross this one on Boones' Exam number 38:
--------------------------------------------------------------
Question 38: What does the following code do?
File f = new File("hello.test");
FileOutputStream out = new FileOutputStream(f);
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.
A) Create a new file named "hello.test" if it does not yet exist. The file is not opened.
b) 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.
c) Open a file named "hello.test" so that you can write to it but cannot read from it.
d) 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.
--------------------------------------------------------------
Now, the first amswer is missing a label, but that's not relavent.
My thinking was, a new File object is created, representing the file. You then create the FileOutputStream using the File object as the constructor. This creates the file if it does not already exist and allows you to write to the file. You are not allowed to read from the file using the code shown. For this you would need to create a FileInputStream using the same file object.
My answer was therefore c as this is the one that matches my answer most closely.
The answer that the answer page gives is:
----------------------------------------------------------------
Question 38: a. The first line creates a File object that represents the file. By creating a FileOutputStream, you create the file if it does not yet exist, and open that file for reading and writing.
----------------------------------------------------------------
Now, for a start, this answer is not answer a, but I guess that there was a mix-up with the numbering. This answer corresponds to the first option.
I had discounted this becasue is says 'write to it and read from it'. I had assumed that the code only lets you write to it.
Any ideas why the given answer is correct - or am I just being picky, or just plain wrong!
Cheers for any replies,
Rich.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think 'b' is the correct answer. It states that file is opened for reading and writing. While the code doesn't create an input stream, nothing in how the file is created prevents one from being used. Try the following code
<pre>
import java.io.*;

public class FileTest
{

public static void main( String args[] )
{
try
{
File file = new File( "testFile.txt" );
FileOutputStream fout = new FileOutputStream( file );
fout.write( "hello".getBytes() );
FileInputStream fin = new FileInputStream( file );
byte b[] = new byte[1024];
fin.read( b );
System.out.println( new String( b ));
fout.close();
fin.close();
}
catch( IOException ioe )
{
ioe.printStackTrace();
}
}
}
</pre>
Yes, I agree that there is a mix up in lettering and they intended b when they said a
Hope This Helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!