chess: (the fire in her eyes masked the fear)
Michelle Taylor ([personal profile] chess) wrote2004-05-09 09:19 pm

Stress burns calories, right?

Have eaten whole 6-pack of scotch pancakes. Have still not finished Tick 7. BufferedReader's readLine method appears to be disagreeing with text files without a line break at the end. Naturally, this text file is one without a line break at the end. There are ways round this, but they are a Royal Pain in the Ass.

It is probably too late for me to be working. Have done no supervision work for tomorrow. This mildly annoys me, because I could have done with doing the maths work, as I'm currently on course for Failing Maths Horribly.

[identity profile] king-of-wrong.livejournal.com 2004-05-09 05:00 pm (UTC)(link)
Surely it's not all that difficult?


FileReader input; int c; String line="";
do {
try { c = input.read(); } catch (java.io.IOException e) { c = -1 }
if (c == -1) break;
if (c == (int)('\n')) break;
line = line + ((char)c);
} while (true);


PS - Watch for the evil blank line at #87, the spurious '\'s and the fact that the calendar line is broken for Easter term...

[identity profile] jaq.livejournal.com 2004-05-10 04:04 am (UTC)(link)
Even if you're reading by character, it's still worth wrapping the FileReader with a BufferedReader for efficiency.

But readLine() works fine for me on a file with no lineend at the end, so I'm not sure why it's a problem.
My example:
public static void main(String[] args) {
try {
BufferedReader rd = new BufferedReader(new FileReader("c:/working/testfile.txt"));
String line;
while((line = rd.readLine()) != null) {
System.out.println(line);
}
System.out.println("[EOF]");
} catch(Exception e) {
System.out.println("caught exception " + e.getMessage());
e.printStackTrace();
}
}

Cool

[identity profile] dr-vannacutt.livejournal.com 2004-05-10 01:55 am (UTC)(link)
I salute your eating a 6-pack of scotch pancakes.

I love those things.