1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

BufferInputStream

Discussion in 'Software Development' started by Caliraj, Jan 23, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    Hi,
    I was going through a program to illustrate the use of BufferedInputStream.The program is as below:
    import java.io.*;
    class printfile
    {
    public static void main(String args[]) throws IOException
    {
    try
    {
    FileInputStream fin=new FileInputStream("hello.java");
    BufferedInputStream b=new BufferedInputStream(fin);
    int ch=0;
    while((ch=b.read())>-1)
    {
    StringBuffer buf=new StringBuffer();
    buf.append((char)ch);
    System.out.print(buf.toString());
    }
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    }
    Now, this is a program to print the particular chosen file in the dos prompt(If im right:confused: )
    Now Im unable to reason out why -1 is present in the "while loop"....I tried replacing -1 with 5,7....the output was same....Then i thought may be its for the caharacter count....so the first line(class hello) occupies 11 columns.....I replaced -1 with 11 expecting the output as the same except that the first line would be omitted.....but the output was only the line!!!!
    I was also told that -1 is used instead of null....so when i replaced the expression as
    while((ch=b.read())!=null), it gave me an error.
    COuld you help me out?
    Thanks,
    Cali
     
  2. IMM

    IMM

    Joined:
    Feb 1, 2002
    Messages:
    3,257
    The read method returns -1 if the end of the stream has been reached.
     
  3. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    Hi,
    Thanks for the reply.But Im unable to understand why it gives the 1st line only as output if i replace -1 with 11. COuld you explain the logic?
    Thanks,
    Cali
     
  4. IMM

    IMM

    Joined:
    Feb 1, 2002
    Messages:
    3,257
    I haven't see your input file - but I'll guess that it is text.
    As you work through that input file - b becomes the character read. (or int == -1 if no char available)
    If this is a text file, the likelihood of encountering a 'character' less than 9 is remote (9 is the tab character) - but you are almost certain to encounter the characters representing the end of a line.
    The Linefeed char (LF) at the end of a line is 10 (0x0A as hex) and the Carriage Return Character (CR) which may be at the end of a line is 13 (0x0D).
    Your test for > 11 (to continue) will fail when you encounter the LF char at the end of the first line.
     
  5. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    FileInputStream and BufferedInputStream are mainly used to read binary files.
    To read a file like "Hello.java" which may be considered as a text file, the appropriate classes are :
    FileReader and BufferedReader

    Example :

    Code:
    [SIZE=2][B]
    String line;
    try {
        FileReader fr = new FileReader("Hello.java");
        BufferedReader br = new BufferedReader(fr);
        line = br.readLine();
        while (line != null) {
            System.out.println(line);
            line = br.readLine();
        }
        br.close();
        fr.close();
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
    } 
    [/B][/SIZE]
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    In your coding, you're reading character by character. Therefore, in each loop of your while statement, the return value of the read method will be 1.

    You may use an array of bytes instead of a character to read your file.

    Exemple :

    Code:
    [SIZE=2][B]
    int bufferSize = 32;    
    byte[] buffer = new byte[bufferSize];  // the array of bytes in which
                                           // the content of the buffered input stream
                                           // will be stored
    int count;                             // counter of bytes being read
    String result;                         // will be used to show the content of the
                                           // array of bytes
    try {
        FileInputStream fis = new FileInputStream("Hello.java");
        BufferedInputStream bis = new BufferedInputStream(fis);
        count = bis.read(buffer, 0, bufferSize);      // the second parameter 0 indicates the offset from
                                                      // which the data will be stored in the array
        while (count != -1) {
            result = new String(buffer, 0, count);
            System.out.println("Counter = " + count + " array content <" + result + ">");
            count = bis.read(buffer, 0, bufferSize);
        }
        bis.close();
        fis.close();
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
    }
    [/B][/SIZE]
     
  7. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    Hi,
    thanks for the answers.I would like to know the difference b/w read() and readChar() methods.Could you let me know the context in which these methods can be used?
    Thanks once again.
    Cali
     
  8. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    readChar() is a method of the interface DataInput and is not used by FileInputStream and BufferedInputStream.
    That method is only suitable for reading bytes written by the writeChar() method of interface DataOutput

    Also, it is not a good idea to use characters while reading a file for the following reason :

    Link : http://java.sun.com/j2se/1.3/docs/api/java/io/DataInputStream.html
     
  9. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    Hi chicon,
    Sorry for this chain of mails.....actually I wanted to know teh difference b/w erad() and readLine() as wella s teh contexts in which they are used......Could you explain the significance of tehse methods?
    Thanks once again,
    Cali
     
  10. IMM

    IMM

    Joined:
    Feb 1, 2002
    Messages:
    3,257
    just a fix for a typo above -- when I said
    b becomes the character read
    it should have been
    ch becomes the character read
     
  11. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    The method read() in a BufferedInputStream returns an integer.
    The method is inherited from the FilerInputStream which is itself an extension of the InputStream class.

    The method readLine() in a BufferedReader returns a string.
    The method is not inherited. BufferedReader is an extension of the Reader class.



    InputStream classes are designed to read bytes.
    Reader classes are designed to read characters.

    OutputStream classes are designed to write bytes.
    Writer classes are designed to write characters.
     
  12. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/537621

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice