Confusion between ZERO char. value and EOF ?!?

Discussion in 'Java' started by Spendius, Jun 10, 2004.

  1. Spendius

    Spendius Guest

    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
     
    Spendius, Jun 10, 2004
    #1
    1. Advertisements

  2. - 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
     
    Gordon Beaton, Jun 10, 2004
    #2
    1. Advertisements

  3. 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
     
    Steve Horsley, Jun 10, 2004
    #3
  4. Spendius

    Liz Guest

    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.
     
    Liz, Jun 11, 2004
    #4
  5. Spendius

    Roedy Green Guest

    I have heard of Ctrl-Z acting as eof, and 0 in C strings, but what OS
    uses null on files?
     
    Roedy Green, Jun 11, 2004
    #5
  6. Spendius

    Liz Guest

    DOS I think.
     
    Liz, Jun 11, 2004
    #6
  7. 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();
    }
    }
     
    Joseph Dionne, Jun 11, 2004
    #7
  8. Spendius

    Roedy Green Guest

    in DOS it was Ctrl-Z.
     
    Roedy Green, Jun 11, 2004
    #8
  9. 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.
     
    Steven J Sobol, Jun 11, 2004
    #9
  10. Spendius

    Roedy Green Guest

    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.
     
    Roedy Green, Jun 11, 2004
    #10
  11. 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
     
    Gordon Beaton, Jun 11, 2004
    #11
  12. Spendius

    Guest Guest

    Give me your curriculum, so I can hire you in my company!

    - Dario
     
    Guest, Jun 11, 2004
    #12
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.