2

I am confused when should I use the combination of FileReader with BufferedReader and when to use FileInputStream for reading a file (and outputting to the log)?

/**
 * Created by mona on 3/26/16.
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
public class FileExample {


    public static void main (String[] args) throws java.io.IOException {

        File newFile = new File("tweet.txt");

        FileWriter fileWriter = new FileWriter(newFile);
        fileWriter.write("Mona Jalal");
        fileWriter.append("\nMona Mona");
        fileWriter.close();
        /*FileReader fileReader = new FileReader(newFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        fileReader.close();
        bufferedReader.close();
        */
        FileInputStream fis = new FileInputStream(newFile);
        System.out.println("fis");
        System.out.println(fis.read()); //prints 77
        int content;
        while ((content=fis.read())!=-1){
            System.out.print((char) content);
        }
        fis.close();

    }
}

I am using Java 8.

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • 1
    FileInputStream is going to read a byte, which you are then casting to a char. The BufferedReader is reading a whole line of text. byte vs text – Scary Wombat Mar 28 '16 at 05:11
  • with both, I can output the same thing. Like the part which is commented and the uncommented part both output the same thing, that is why I am confused – Mona Jalal Mar 28 '16 at 05:12
  • 1
    They do the same thing--it's more about efficiency. Raw image, audio, video data would use FileInputStream. In your case, you retrieve text, so you would be better off with BufferedReader – jrhee17 Mar 28 '16 at 05:17

3 Answers3

1

For reading raw bytes: FileInputstream https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html

A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.


For reading characters, arrays, lines efficiently (using a buffer) https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

BufferedReader in = new BufferedReader(new FileReader("foo.in"));

will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

Java has extremely good documentation!

jrhee17
  • 1,152
  • 9
  • 19
1

Reader and it's extending classes

java.io.Reader
    java.io.InputStreamReader
         java.io.FileReader

Assume the input stream is a text and do the casting to (char) automatically.

InputStream and it's subclasses

java.io.InputStream
    java.io.FileInputStream

Make no assumptions and only read bytes.

If you know it's a text file - use Reader, If you can't assume - use InputStream.

Daniel
  • 6,194
  • 7
  • 33
  • 59
1
public class FileReader    extends InputStreamReader

Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

public class BufferedReader extends Reader

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

 BufferedReader in  = new BufferedReader(new FileReader("foo.in"));

will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.

Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
  • @Mona Specifically for your confusion you can read this nice explanation http://stackoverflow.com/a/9648877/3514144 – Ajay Pandya Mar 28 '16 at 05:27