1

I got a doubt when i was going through a program in order to read a file.We can read a file by using BufferedReader which is as follows

package com.mkyong.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("C:\\testing.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}

And we can also read a file by using BufferedInputStream which is as follows

package com.mkyong.io;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamExample {

    public static void main(String[] args) {

        File file = new File("C:\\testing.txt");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        try {
            fis = new FileInputStream(file);

            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);

            while (dis.available() != 0) {
                System.out.println(dis.readLine());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                bis.close();
                dis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

So my doubt is what is difference in using BufferedReader and BufferedInputStream and which is more efficient

str
  • 42,689
  • 17
  • 109
  • 127
FIFA oneterahertz
  • 718
  • 2
  • 16
  • 43
  • 1
    I think you should tag this question with Java and not JavaScript to get relevant answers. – Prahlad Yeri Mar 30 '16 at 06:14
  • 1
    Streams are for binary data: they read and write bytes. Readers are for textual data: they read and write characters. If you want to read text, use a Reader. And don't ever use available(): it doesn't do what you think it does. – JB Nizet Mar 30 '16 at 06:17
  • @PrahladYeri I used java,file-io,bufferedreader,fileinputstream,bufferedinputstream tags only.I dont see anywhere whether i tagged javascript – FIFA oneterahertz Mar 30 '16 at 06:19
  • @JBNizet I know that readLine() method of datainputstream is deprecated so i dont use available() method as well but my doubt is if we use BufferedInputStream to read a file and BufferedReader to read a file i am getting the same output.Then why java fellows introduced both bufferedreader and bufferedinputstream they could have introduced any one of those to avoid confusion. – FIFA oneterahertz Mar 30 '16 at 06:27
  • 1
    It's deprecated because it should never have existed in the first place. DataInputStream.readLine() considers each byte to be a character. That's not true with most character encodings. It basically uses ASCII, always, doesn't honor the platform encoding nor support any other. Readers are used to properly read characters, with proper support for character encoding. You also assume that all you need to do with character streams is to read line by line, but that's not the only use-case either. So there is really no confusion: streams for bytes, readers/writers for characters. – JB Nizet Mar 30 '16 at 06:35
  • @JBNizet Now i understood the difference.The diiference is character encodings.Thank You for helping me to understand the difference – FIFA oneterahertz Mar 30 '16 at 06:50

0 Answers0