Skip to main content

Java IO File Reader/Scanner Question

2 replies [Last post]
liamshannon
Offline
Joined: 2011-07-12
Points: 0

Hi I was given this class and method and asked to:

" Please list your observations about this class, including bugs, poor programming, inefficiencies, etc..."

And wrote an alternative version using a Scanner instead and identified the forward slashes should be back slashes and escaped and provided my own readFileFixed() method, however I was told that this wasn't a very good solution and that I should go off and think a bit more about it, but cant really think of anything else and would be really greatful for any hints, tips or Advice....

{code}

package codeTest;

import java.io.File;
import java.io.FileReader;

public class CodeTestExerciseBadCode {

public CodeTestExerciseBadCode(){

}
/**
* @param args
*/
public static void main(String[] args) {
CodeTestExerciseBadCode part2 = new CodeTestExerciseBadCode();
System.out.println(part2.readFile());
}

public String readFile(){
File f = null;
FileReader fr = null;
StringBuffer content = null;
try{
f = new File("c:/samplefile.txt");
fr = new FileReader(f);

int c;
while((c = fr.read()) != -1){
if(content == null){
content = new StringBuffer();
}

content.append((char)c);
}

fr.close();
}
catch (Exception e) {
throw new RuntimeException("An error occured reading your file");
}

return content.toString();
}
}

{code}

And my initial suggested solution:

{code}

public String readFileFixed() {

StringBuffer content;
content = new StringBuffer();

File file = new File("C:\\here\\sampletextfile.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
content.append(scanner.nextLine());
content.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
e.printStackTrace();
}

return content.toString();
}

{code}

So any suggestions would be great, thanks...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bwillcott
Offline
Joined: 2007-06-01
Points: 0

Buffering is certainly needed when dealing will large files, however, I don't believe it
is needed all the time.

I have modified and commented the code you submitted:

//
// CodeTestExerciseBadCode.java
//

//~--- JDK imports ------------------------------------------------------------

import java.io.File;
import java.io.FileReader;

import java.util.Scanner;

//~--- classes ----------------------------------------------------------------

public class CodeTestExerciseBadCode {

/**
* It is alway better to use static final variables<br>
* than in-line string constants. (BW)
*/
private static final String testFilename = "CodeTestExerciseBadCode.java";

//~--- constructors -------------------------------------------------------

public CodeTestExerciseBadCode() {}

//~--- methods ------------------------------------------------------------

/**
* @param args
*/
public static void main(String[] args) {
CodeTestExerciseBadCode part2 = new CodeTestExerciseBadCode();

System.out.println(part2.readFileFixed());
}

/**
* Personally, I see nothing wrong with this code<br>
* apart from the lack of the few lines I added to<br>
* {@linkplain #readFileFixed() readFileFixed()}. (BW)
*
* @return
*/
public String readFile() {
File f = null;
FileReader fr = null;
StringBuffer content = null;

try {
f = new File(testFilename);
fr = new FileReader(f);

int c;

while ((c = fr.read()) != -1) {
if (content == null) {
content = new StringBuffer();
}

content.append((char) c);
}

fr.close();
} catch (Exception e) {
// Sometimes you need the cause as well to understand
// what went wrong (BW)
throw new RuntimeException("An error occured reading your file", e);
}

return content.toString();
}

/**
* As modified by Brad Willcott (BW)
*
* @return
*/
public String readFileFixed() {
StringBuffer content = null;
File file = new File(testFilename);

// I would add this test so that if a directory
// name is provided, then you won't have a problem.
// (BW)
if (file.isFile() && (file.length() > 0)) {

// By putting this inside the if() block,
// you avoid creating it unnecessarily if
// you don't need it. (BW)
content = new StringBuffer();

try {
Scanner scanner = new Scanner(file);

while (scanner.hasNextLine()) {
content.append(scanner.nextLine());
content.append(System.getProperty("line.separator"));
}
} catch (Exception e) {

// Throwing an exception is better than just printing
// the stack trace. (BW)
throw new RuntimeException("An error occured reading your file", e);
}
}

// I would add this (BW)
return((content == null)
? ""
: content.toString());
}
}

wrandelshofer
Offline
Joined: 2004-11-11
Points: 0

Hi,

I suggest you take a look at buffered reading from files.

A good read is the following article by Oracle: Tuning Java I/O performance