How to detect End-of-File?

  • Thread starter newbie_at_sendmail
  • Start date
N

newbie_at_sendmail

Hi,

I'm quite new to java and I have huge problems doing something as simple as reading a file (up to end-of-file).
Can anybody please tell me what I'm doing wrong/is missing? I can't figure it out.

Thanks in advance.

package end_of_file;

import java.lang.String;
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
import java.io.EOFException;
import static java.lang.System.out;

public class this_will_never_work
{
public static void main(String args[]) throws IOException
{
Scanner myScanner =
new Scanner(new File("c:\\temp\\this_will_never_work.txt"));
while (true)
{
try
{
String regel = myScanner.nextLine();
out.println("Gelezen regel= "+regel);
throw EOFException;
} catch (EOFException e) {out.println("Laatste regel gelezen");}
}
}
}


--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-
 
K

Knute Johnson

newbie_at_sendmail said:
Hi,

I'm quite new to java and I have huge problems doing something as simple as reading a file (up to end-of-file).
Can anybody please tell me what I'm doing wrong/is missing? I can't figure it out.

Thanks in advance.

package end_of_file;

import java.lang.String;
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
import java.io.EOFException;
import static java.lang.System.out;

public class this_will_never_work
{
public static void main(String args[]) throws IOException
{
Scanner myScanner =
new Scanner(new File("c:\\temp\\this_will_never_work.txt"));
while (true)
{
try
{
String regel = myScanner.nextLine();
out.println("Gelezen regel= "+regel);
throw EOFException;
} catch (EOFException e) {out.println("Laatste regel gelezen");}
}
}
}

I'm not sure that Scanner is the most convenient class or practical
class to use to get data from a random text file. If the file is some
sort of structured data then it does make some sense. What type of data
is the file and what are you going to do with it?

import java.io.*;
import java.util.*;

public class test {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(new File("test.java"));
while (s.hasNext())
System.out.println(s.next());
}
}
 
Z

Zefria

java.io.BufferedReader is also a possibility. It wraps around a
java.io.FileReader. The code might look something like this:

public void FileReadingMethod(String filename)
{
BufferedReader br = null;
String line;
try
{
br = new BufferedReader(new FileReader(filename));
}
catch (FileNotFoundException e)
{
System.err.println("Requested file was not found!");
System.exit(1);
}
try
{
line = br.readLine();
while (true)
{
/////////
// PROCESSING CODES GOES HERE
/////////

// Read the next line. If line==null, that indicates the end of the
file.
line = br.readLine();
if (line == null) break;
}
}
catch (IOException e)
{
System.err.println("File was found, but an IOError occured!");
System.exit(2);
}
}
 
E

Esmond Pitt

Zefria said:
java.io.BufferedReader is also a possibility. It wraps around a
java.io.FileReader. The code might look something like this:

public void FileReadingMethod(String filename)
{
BufferedReader br = null;
String line;
try
{
br = new BufferedReader(new FileReader(filename));
}
catch (FileNotFoundException e)
{
System.err.println("Requested file was not found!");
System.exit(1);
}
try
{
line = br.readLine();
while (true)

if (line == null) break; is missing here before the 'while'.

As you have to code the read and the test twice in this style, the loop
is generally written like this:

String line;
while ((line = br.readLine() != null)
{
// ...
}

or

for (String line; (line = br.readLine()) != null;)
{
// ...
}
 
D

Daniel Gee

Ah, I had forgotten about the possibility of an empty file. I guess
the code is better written as:

try
{
line = br.readLine();
while (true)
{
if (line == null) break;

/////////
// PROCESSING CODES GOES HERE
/////////

line = br.readLine();
}
}
catch(Exception e){...}

As for that "while( (line = br.readLine() != null )" nonsense, the
Python programmer in me knows otherwise.
 
G

Gordon Beaton

line = br.readLine();
while (true)
{
if (line == null) break;
[...]

As for that "while( (line = br.readLine() != null )" nonsense, the
Python programmer in me knows otherwise.

Feel free to ignore the idiom, but at least put the loop condition
where it belongs:

try {
line = br.readLine();
while (line != null) {
//////
// PROCESSING CODES GOES HERE
//////
line = br.readLine();
}
catch (Exception e) {...}

/gordon

--
 
L

Lew

Gordon said:
Feel free to ignore the idiom, but at least put the loop condition
where it belongs:

try {
line = br.readLine();
while (line != null) {
//////
// PROCESSING CODES GOES HERE
//////
line = br.readLine();
}
catch (Exception e) {...}

or equivalently,

for ( line = br.readLine(); line != null; line = br.readLine() ) ...
 
E

Esmond Pitt

Lew said:
or equivalently,

for ( line = br.readLine(); line != null; line = br.readLine() ) ...

or equivalently,

for (String line; (line = br.readline()) != null;) ...
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Esmond said:
As you have to code the read and the test twice in this style, the loop
is generally written like this:

String line;
while ((line = br.readLine() != null)
{
// ...
}

or

for (String line; (line = br.readLine()) != null;)
{
// ...
}

I would call the first the standard way of coding it.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Gordon said:
line = br.readLine();
while (true)
{
if (line == null) break;
[...]

As for that "while( (line = br.readLine() != null )" nonsense, the
Python programmer in me knows otherwise.

Feel free to ignore the idiom, but at least put the loop condition
where it belongs:

try {
line = br.readLine();
while (line != null) {
//////
// PROCESSING CODES GOES HERE
//////
line = br.readLine();
}
catch (Exception e) {...}

Two readLine's will never convince me as being a better
solution than one.

Arne
 
L

Luc The Perverse

Esmond Pitt said:
or equivalently,

for (String line; (line = br.readline()) != null;) ...

or equivalently,

String line;
while((line = br.readline()) != null) . . .
 
L

Luc The Perverse

Ah! How did I miss this???

Now I look like an idiot reposting it

GRRR

Sorry guys (gals notwithstanding) - I will try to contribute more
attentively.
 
C

Chris Uppal

Arne said:
Two readLine's will never convince me as being a better
solution than one.

Agreed entirely. Some people may find the assign-in-the-test idiom not quite
to their taste, but that /is/ a matter of taste, and it is at worst an
established idiom. But duplication is more than unconventional, it is more
than ugly -- it is /wrong/.

-- chris
 
G

Gordon Beaton

Agreed entirely. Some people may find the assign-in-the-test idiom
not quite to their taste, but that /is/ a matter of taste, and it is
at worst an established idiom. But duplication is more than
unconventional, it is more than ugly -- it is /wrong/.

Just to be clear - I'm with both of you on this. My comment was
directed at the *other* bad smell in the earlier post:

while (true) {
if (something) break;

// other stuff
}

/gordon

--
 
D

Daniel Gee

Agreed entirely. Some people may find the assign-in-the-test idiom not quite
to their taste, but that /is/ a matter of taste, and it is at worst an
established idiom. But duplication is more than unconventional, it is more
than ugly -- it is /wrong/.

Ah, well, you see, it's not always a matter of taste. In python you
are strictly not allowed to make an assignment when a boolean test is
being made, such as for the while loop. It's even in the FAQ:
http://www.python.org/doc/faq/general/#why-can-t-i-use-an-assignment-in-an-expression

Anyways, it's just a habit to do it however you do it I guess. All the
options are fairly obvious about what they're doing, so even if others
are reading your code they're very unlikely to be lost in it.
 
E

Esmond Pitt

Arne said:
I would prefer standard code style over limited scope any day.

I would say any time they are in conflict you don't really have anything
you could reasonably describe as being 'standard'.
 
S

squirrel

line = br.readLine();
while (true)
{
if (line == null) break;
[...]

As for that "while( (line = br.readLine() != null )" nonsense, the
Python programmer in me knows otherwise.

Feel free to ignore the idiom, but at least put the loop condition
where it belongs:

try {
line = br.readLine();
while (line != null) {
//////
// PROCESSING CODES GOES HERE
//////
line = br.readLine();
}
catch (Exception e) {...}

/gordon

--

why not use do...while loop as the following:

String line = null;
try {
if
do {
//////
// PROCESSING CODES GOES HERE
//////
line = br.readLine();
}
while (line != null);
catch (Exception e) {...}

of course, the pre-condition is the buffer not empty.
 
G

Guest

Esmond said:
I would say any time they are in conflict you don't really have anything
you could reasonably describe as being 'standard'.

Not true.

This is a good example.

The while loop is tha standard way of writing that code
in Java (actually in C# too).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top