read

Discussion in 'Java' started by misiek, Jul 19, 2005.

  1. misiek

    misiek Guest

    Hello!
    I`ve got problem.
    I tried to read data from file using FileInputStream and 'read' method...
    I don`t exactly know how to do it.
    I have to use 4096 bytes blocks.
    sorry for my english

    Thanks

    --
    Majs
     
    misiek, Jul 19, 2005
    #1
    1. Advertising

  2. misiek

    Guest

    , Jul 19, 2005
    #2
    1. Advertising

  3. misiek

    misiek Guest

    wrote:

    > According to
    >

    http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html#read(byte[])
    > you just call read() with a byte array, and whatever the size of your
    > array, that's how many bytes you'll read, though you have to check
    > return status


    Hey!
    I found this example but it just doesn`t work...I can`t see last part of the
    file I`m reading

    byte [] bufor = new byte [4096];
    int bytesRead = 0;
    int offset = 0;

    while(offset < dlugosc)
    {
    //bytesRead = is.read(bufor,offset,bufor.length - offset);
    bytesRead = is.read(bufor);
    if(bytesRead == -1) break;
    offset += bytesRead;
    System.out.println(new String(bufor));
    }
    is.close();
     
    misiek, Jul 19, 2005
    #3
  4. misiek

    misiek Guest

    misiek wrote:

    > wrote:
    >
    >> According to
    >>

    >

    http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html#read(byte[])
    >> you just call read() with a byte array, and whatever the size of your
    >> array, that's how many bytes you'll read, though you have to check
    >> return status

    >
    > Hey!
    > I found this example but it just doesn`t work...I can`t see last part of
    > the file I`m reading
    >
    > byte [] bufor = new byte [4096];
    > int bytesRead = 0;
    > int offset = 0;
    >
    > while(offset < dlugosc)
    > {
    > //bytesRead = is.read(bufor,offset,bufor.length - offset);
    > bytesRead = is.read(bufor);
    > if(bytesRead == -1) break;
    > offset += bytesRead;
    > System.out.println(new String(bufor));
    > }
    > is.close();


    dlugosc = File.length() ;)
     
    misiek, Jul 19, 2005
    #4
  5. wrote:
    > According to
    > http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html#read(byte[])
    > you just call read() with a byte array, and whatever the size of your
    > array, that's how many bytes you'll read,


    Not at all. You are just guaranteed that you will not read more bytes
    than what fits in the array. You can get less. You can even get zero.

    > though you have to check
    > return status


    The return value is not just a status value. It tells you how many bytes
    you indeed got. Use this value if you further process the array data,
    never just the size of the array.

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Jul 19, 2005
    #5
  6. misiek wrote:
    >>while(offset < dlugosc)
    >> {
    >> //bytesRead = is.read(bufor,offset,bufor.length - offset);
    >> bytesRead = is.read(bufor);
    >> if(bytesRead == -1) break;
    >> offset += bytesRead;
    >> System.out.println(new String(bufor));
    >> }
    >>is.close();

    >
    >
    > dlugosc = File.length() ;)


    (a) Using File.length() is a bad idea. File sizes can and do change
    while you read. E.g. all it takes is that someone deletes the file
    between the moment you called File.length() and while you use the data.

    (b) Using binary I/O when you want to do text I/O is a rather bad idea, too.

    (c) Converting the buffer to a string without taking the just read
    'bytesRead' size into account is a bad idea. The array might only be
    half filled, but the String() constructor doesn't know about this.

    (d) Doing the conversion (which should not be done, see (b)), without
    specifying a charset is a bad idea. Java will use the platform's default
    which might or might not the the right one.


    Please work through Sun's I/O tutorial at

    http://java.sun.com/docs/books/tutorial/essential/io/index.html

    Oh, and beginners questions are best asked in comp.lang.java.help

    /Thomas


    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Jul 19, 2005
    #6
  7. misiek

    misiek Guest

    Thomas Weidenfeller wrote:

    > Oh, and beginners questions are best asked in comp.lang.java.help
    >
    > /Thomas
    >
    >

    thanks ;)
     
    misiek, Jul 19, 2005
    #7
  8. misiek

    Roedy Green Guest

    On Tue, 19 Jul 2005 14:32:50 +0200, misiek <>
    wrote or quoted :

    >I tried to read data from file using FileInputStream and 'read' method...
    >I don`t exactly know how to do it.
    >I have to use 4096 bytes blocks.


    see http://mindprod.com/applets/fileio.html

    Just tell it what you want to and it will write the code for you.

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 23, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Isaac
    Replies:
    0
    Views:
    2,480
    Isaac
    Jul 10, 2003
  2. Doug
    Replies:
    3
    Views:
    4,410
    Bruce Barker
    Nov 4, 2005
  3. Replies:
    3
    Views:
    550
    John W. Krahn
    Apr 7, 2006
  4. Steve
    Replies:
    6
    Views:
    3,714
    Steve
    May 13, 2004
  5. lokb
    Replies:
    5
    Views:
    4,697
    Victor Bazarov
    Jul 8, 2004
Loading...

Share This Page