Assuming the port statements are correct, which two code fragments create a one-byte file?

Assuming the port statements are correct, which two code fragments create a one-byte file?

Assuming the port statements are correct, which two code fragments create a one-byte file?

A.
OutputStream fos = new FileOutputStream(new File(“/tmp/data.bin”)); OutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeByte(0);
dos.close();

B.
OutputStream fos = new FileOutputStream (“/tmp/data.bin”); dataOutputStream dos = new DataOutputStream(fos);
dos.writeByte(0);
dos.close();

C.
OutputStream fos = new FileOutputStream (new File (“/tmp/data.bin”)); dataOutputStream dos = new DataOutputStream(os);
dos.writeByte(0);
dos.close();

D.
OutputStream fos = new FileOutputStream (“/tmp/data.bin”); fos.writeByte(0);
fos.close();

Explanation:
B:Create DataOutputStream from FileOutputStream public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputS tream(“C:/demo.txt”); DataOutputStream dos = new DataOutputStream(fos);

Note:
The FileOutputStream class is a subclass of OutputStream. You can construct a FileOutputStream object by passing a string containing a path name or a File object.
You can also specify whether you want to append the output to an existing file.
public FileOutputStream (String path)
public FileOutputStream (String path, boolean append)
public FileOutputStream (File file)
public FileOutputStream (File file, boolean append)

With the first and third constructors, if a file by the specified name already exists, the file will be overwritten. To append to an existing file, pass true to the second or fourth constructor.

Note 2:
public class DataOutputStream
extends FilterOutputStream implements DataOutput

A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.

Reference:java.io Class DataOutputStream



Leave a Reply 10

Your email address will not be published. Required fields are marked *


Kamil

Kamil

First example A) also work fine and create one-byte file.
Who know why A is not correct answer?

Francesco

Francesco

the B and C answers are incorrect as they have dataOutputStream instead of DataOutputStream. The other two works correctly ( A and D )

Mohana

Mohana

Hi all,

I plan to take this exam in next week.. will these questions will be appear for that exam.. Please if you have any latest materials kindly forward to my ID [email protected]..

Thanks in Advance,
Mohana

admin

admin

Good luck to you!

Yury

Yury

D is incorrect because OutputStream doesn’t contain method writeByte.

Charlie

Charlie

If B & C have DataOutputStream not dataOutputStream, and C has fos not os, then answer is ABC

Marian

Marian

Any of a b c produces a 1 byte file

Tim

Tim

I tried A, and it produces a 1-byte file too.

David

David

A) It’s correct answer
B) It’s correct answer –> If we replace dataOutputStream by DataOutputStream
C) It’s correct answer –> If we replace dataOutputStream by DataOutputStream and os by dos
D) It isn’t correct answer –> because OutputStream doesn’t contain method writeByte.