Should I use DataInputStream or BufferedInputStream

前端 未结 7 933
南方客
南方客 2020-12-13 20:55

I want to read each line from a text file and store them in an ArrayList (each line being one entry in the ArrayList).

So far I understand that a BufferedInputStream

相关标签:
7条回答
  • 2020-12-13 21:29

    You can always use both:

    final InputStream inputStream = ...;
    final BufferedInputStream bufferedInputStream =
            new BufferedInputStream(inputStream);
    final DataInputStream dataInputStream =
            new DataInputStream(bufferedInputStream);
    
    0 讨论(0)
  • 2020-12-13 21:30

    The two classes are not mutually exclusive - you can use both of them if your needs suit.

    As you picked up, BufferedInputStream is about reading in blocks of data rather than a single byte at a time. It also provides the convenience method of readLine(). However, it's also used for peeking at data further in the stream then rolling back to a previous part of the stream if required (see the mark() and reset() methods).

    DataInputStream/DataOutputStream provides convenience methods for reading/writing certain data types. For example, it has a method to write/read a UTF String. If you were to do this yourself, you'd have to decide on how to determine the end of the String (i.e. with a terminator byte or by specifying the length of the string).

    This is different from BufferedInputStream's readLine() which, as the method sounds like, only returns a single line. writeUTF()/readUTF() deal with Strings - that string can have as many lines it it as it wants.

    BufferedInputStream is suitable for most text processing purposes. If you're doing something special like trying to serialize the fields of a class to a file, you'd want to use DataInput/OutputStream as it offers greater control of the data at a binary level.

    Hope that helps.

    0 讨论(0)
  • 2020-12-13 21:31

    The differences are:

    • The DataInputStream works with the binary data, while the BufferedReader work with character data.

    • All primitive data types can be handled by using the corresponding methods in DataInputStream class, while only string data can be read from BufferedReader class and they need to be parsed into the respective primitives.

    • DataInputStream is a part of filtered streams, while BufferedReader is not.

    • DataInputStream consumes less amount of memory space being it is a binary stream, whereas BufferedReader consumes more memory space being it is character stream.

    • The data to be handled is limited in DataInputStream, whereas the number of characters to be handled has wide scope in BufferedReader.

    0 讨论(0)
  • 2020-12-13 21:38

    Use a normal InputStream (e.g. FileInputStream) wrapped in an InputStreamReader and then wrapped in a BufferedReader - then call readLine on the BufferedReader.

    DataInputStream is good for reading primitives, length-prefixed strings etc.

    0 讨论(0)
  • 2020-12-13 21:42

    I would advocate using Jakarta Commons IO and the readlines() method (of whatever variety).

    It'll look after buffering/closing etc. and give you back a list of text lines. I'll happily roll my own input stream wrapping with buffering etc., but nine times out of ten the Commons IO stuff works fine and is sufficient/more concise/less error prone etc.

    0 讨论(0)
  • 2020-12-13 21:44

    You shoud use DataInputStream in cases when you need to interpret the primitive types in a file written by a language other Java in platform-independent manner.

    0 讨论(0)
提交回复
热议问题