Confusion between ZERO char. value and EOF ?!?

S

Spendius

Hi,
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)

I have a file containing integers:
2375 2485 6323
2485 2375 6323
2376 2377 3659
2318 4436 0
2377 2376 3659
2377 2399 3755
2399 2377 3755
....
thru which I loop using
File f = new File("./values.txt");
BufferedReader in = new BufferedReader(new FileReader(f));
then
while ((line = in.readLine()) != null)
{ ...
I've noticed I get out of the loop (and thus from the sequential
reading of my file) when I encounter a line ending with '0' (ZERO
value). I've tried different ways of instantiating my InputStream
but to no avail... The break out of the loop really happens when
I stumble against this 0, I've checked it by moving my 4rd line
above to other positions in the file and every time I loop as
many times as I read non-ending-with-0 lines...
How can you explain that ? How could I deal with this funny
behaviour ? Isn't it a bug or something ?
Thanks !
Regards,
Spendius
 
G

Gordon Beaton

I have a file containing integers:
2375 2485 6323
2485 2375 6323
2376 2377 3659
2318 4436 0
2377 2376 3659
2377 2399 3755
2399 2377 3755
...
thru which I loop using
File f = new File("./values.txt");
BufferedReader in = new BufferedReader(new FileReader(f));
then
while ((line = in.readLine()) != null)
{ ...

I've noticed I get out of the loop (and thus from the sequential
reading of my file) when I encounter a line ending with '0' (ZERO
value).

- does the file contain just text, or are the integers stored in some
other format?

- did you cut and paste the above code, or retype it in your post?

- what does the rest of your read loop look like? The fragment you've
posted won't terminate until the end of the file is reached. Post a
complete, self-contained example that has this problem.

$ cat ./values.txt
2375 2485 6323
2485 2375 6323
2376 2377 3659
2318 4436 0
2377 2376 3659
2377 2399 3755
2399 2377 3755

$ java Read
2375 2485 6323
2485 2375 6323
2376 2377 3659
2318 4436 0
2377 2376 3659
2377 2399 3755
2399 2377 3755

$ cat Read.java
import java.io.*;

public class Read {
public static void main(String[] args) throws Exception {
File f = new File("./values.txt");
BufferedReader in = new BufferedReader(new FileReader(f));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
}

/gordon
 
S

Steve Horsley

Spendius said:
Hi,
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)

I have a file containing integers:
2375 2485 6323
2485 2375 6323
2376 2377 3659
2318 4436 0
2377 2376 3659
2377 2399 3755
2399 2377 3755
...
thru which I loop using
File f = new File("./values.txt");
BufferedReader in = new BufferedReader(new FileReader(f));
then
while ((line = in.readLine()) != null)
{ ...
I've noticed I get out of the loop (and thus from the sequential
reading of my file) when I encounter a line ending with '0' (ZERO
value). I've tried different ways of instantiating my InputStream
but to no avail... The break out of the loop really happens when
I stumble against this 0, I've checked it by moving my 4rd line
above to other positions in the file and every time I loop as
many times as I read non-ending-with-0 lines...
How can you explain that ? How could I deal with this funny
behaviour ? Isn't it a bug or something ?
Thanks !
Regards,
Spendius

It is very probably a bug, but I don't think you have posted the
code that contains the bug. I guess it's a little bit further down
the loop.

Can you post a small but complete demo program that shows the
problem?

Steve
 
L

Liz

Steve Horsley said:
It is very probably a bug, but I don't think you have posted the
code that contains the bug. I guess it's a little bit further down
the loop.

Can you post a small but complete demo program that shows the
problem?

Steve

In some systems a 'text' file is considered to be terminated
by a zero byte in the file 0x00, not the character '0'.
So I guess that it is your operating system that is telling
java that the end of the file has been reached.
 
R

Roedy Green

In some systems a 'text' file is considered to be terminated
by a zero byte in the file 0x00, not the character '0'.
I have heard of Ctrl-Z acting as eof, and 0 in C strings, but what OS
uses null on files?
 
J

Joseph Dionne

Spendius said:
Hi,
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)

I have a file containing integers:
2375 2485 6323
2485 2375 6323
2376 2377 3659
2318 4436 0
2377 2376 3659
2377 2399 3755
2399 2377 3755
...
thru which I loop using
File f = new File("./values.txt");
BufferedReader in = new BufferedReader(new FileReader(f));
then
while ((line = in.readLine()) != null)
{ ...
I've noticed I get out of the loop (and thus from the sequential
reading of my file) when I encounter a line ending with '0' (ZERO
value). I've tried different ways of instantiating my InputStream
but to no avail... The break out of the loop really happens when
I stumble against this 0, I've checked it by moving my 4rd line
above to other positions in the file and every time I loop as
many times as I read non-ending-with-0 lines...
How can you explain that ? How could I deal with this funny
behaviour ? Isn't it a bug or something ?
Thanks !
Regards,
Spendius

Works for me dos or unix file formats.

import java.io.*;

public class Test
{
public static void main(String[] args) throws Throwable {
Test test = new Test();

test.makeInput("./values.txt");
test.readFile("./values.txt");
}

void makeInput(String fileName) throws Throwable {
PrintStream ps = new PrintStream(new FileOutputStream(fileName),true);

ps.println("2375 2485 6323");
ps.println("2485 2375 6323");
ps.println("2376 2377 3659");
ps.println("2318 4436 0");
ps.println("2377 2376 3659");
ps.println("2377 2399 3755");
ps.println("2399 2377 3755");

ps.close();
}

void readFile(String fileName) throws Throwable {
File f = new File(fileName);
BufferedReader in = new BufferedReader(new FileReader(f));
String line;

while((line = in.readLine()) != null) {
System.out.println(line);
}

in.close();
}
}
 
S

Steven J Sobol

Roedy Green said:
in DOS it was Ctrl-Z.

DOS and Unix both terminate files with a NUL character (ASCII value=0).
The difference is that when you're entering data into a text file at the
command prompt, ^Z signifies end of file in DOS and ^D signifies EOF in Unix.
 
R

Roedy Green

DOS and Unix both terminate files with a NUL character (ASCII value=0).
The difference is that when you're entering data into a text file at the
command prompt, ^Z signifies end of file in DOS and ^D signifies EOF in Unix.

Maybe you are thinking of CPM? DOS kept file sizes in the directory.
Occasionally you also found a ^Z at the end of file as well.
 
G

Gordon Beaton

DOS and Unix both terminate files with a NUL character (ASCII
value=0).

I don't know where you heard that about unix, but there is no such
terminating character on any unix I've heard of.

On Unix, a file is essentially a flat byte array. The OS does not
impose any structure or semantics on the contents of the file. There
is no terminating character, and any of the 256 byte values can be
stored in the file. None will cause premature EOF.

/gordon
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top