What change should you make to this code to read and write strings instead of character arrays?

Given the code fragment:

public void ReadFile (String source) {

char[] c = new char [128];

int cLen = c.length;

try (FileReader fr = new FileReader (source)) {

int count = 0;

int read = 0;

while ((read = fr.read(c)) != -1) {

count += read;

}

System.out.println(“Read: ” + count + ” characters.”);

} catch (IOException i) {

}

What change should you make to this code to read and write strings instead of character arrays?

Given the code fragment:

public void ReadFile (String source) {

char[] c = new char [128];

int cLen = c.length;

try (FileReader fr = new FileReader (source)) {

int count = 0;

int read = 0;

while ((read = fr.read(c)) != -1) {

count += read;

}

System.out.println(“Read: ” + count + ” characters.”);

} catch (IOException i) {

}

What change should you make to this code to read and write strings instead of character arrays?

A.
ChangeFileReader to Readers.

B.
ChangeFileReader to DataReader.

C.
ChangeFileReader to File.

D.
ChangeFileReader to BufferReader.

Explanation:
public class BufferedReader
extends Reader
Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.



Leave a Reply 5

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


Jav

Jav

ChangeFileReader to BufferedReader.

Tim

Tim

+1, there is no “DataReader” (only “DataInputStream” available).

Surfo

Surfo

The answer is D.

Amit

Amit

Hi All,

I’m going to write an exam of Java SE 8 Programmer-II[1z0-809] on 08-Jan-2017.
If you guys have this dump please send me on [email protected] . It will be great help me. Thank you so much.

saisha

saisha

Hi I am planning to write 1z0-804. Anybody has dump material then please send it to me at [email protected]. I will be very grateful…. plzz