Results 1 to 7 of 7

Thread: Unbuffered I/O

  1. #1
    Farzaneh is offline Member
    Join Date
    Aug 2008
    Posts
    8
    Rep Power
    0

    Default Unbuffered I/O

    Hi Every one,

    I need to read unbuffered I/O in java. could any one send me a peice of code that reads a line with unbuffered I/O ? or give me tips on how to do this?

    thanks,

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Not good actually, but this way is fine...

    Java Code:
    InputStream is = new BufferedInputStream(new FileInputStream(fs));

  3. #3
    Farzaneh is offline Member
    Join Date
    Aug 2008
    Posts
    8
    Rep Power
    0

    Default

    Thank you Eranga! But I was wonderring isn't what you said a BUFFERED I/O?(because of BufferedInputStream)

    I read it somewhere we should use something like
    InputStream in = new FileInputStream(f);
    int b;
    b = in.read();

    that I think it must read byte by byte, but when I want to use such code in my program I couldn't enter anything to it. and I need to read a line that I don't know how to do this with this method :(
    any suggestion or your method is really UNbuffered I/O ?(it will be great if so :) )
    I should write an external acl_helper for Squid with java and it works only with unbuffered I/O.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    The way I suggest and the way you are talking is same. But the different is safer way you use.


  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    I need to read unbuffered I/O in java
    Why the unbuffered requirement?
    I need to read a line that I don't know how to do this with this method
    If the definition of a "line" is a bunch of characters followed by a lineend character, then you can use the read() method to read bytes until you get the lineend char. Put the bytes into an array and use one of the String constructors to convert the array of bytes to a String. You might have to consider what character set to use.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.io.*;
    
    public class ReadTest {
        public static void main(String[] args) throws IOException {
            File file = new File("ReadTest.java");
            InputStreamReader isr = new InputStreamReader(
                                    new FileInputStream(file));
            System.out.println("character encoding = " + isr.getEncoding());
            char newLine = '\n';
            int lineCount = 0;
            int c;
            while( (c = isr.read()) != -1 ) {
                System.out.print((char)c);
                if((char)c == newLine) {
                    lineCount++;
                }
                // Stop reading after the fifth line.
                if(lineCount == 5) {
                    break;
                }
            }
            isr.close();
            System.out.println("lines read = " + lineCount);
        }
    }

  7. #7
    Farzaneh is offline Member
    Join Date
    Aug 2008
    Posts
    8
    Rep Power
    0

    Default

    Thank you very much all! It really helped me, especially "hardwired"'s code:)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •