Java RandomAccessFile vs. DataInputStream for byte operations

后端 未结 2 1203
你的背包
你的背包 2020-12-21 13:54

I need to read bytes from a file.
Is there a difference (e.g. efficiency, memory, runtime, complexity and inelegance of code) between using RandomAccessFile

相关标签:
2条回答
  • 2020-12-21 14:33

    DataInputStream/DataOutputStream is totally fine if you only need to read/write it sequentally.

    If you need random access (like to an array of bytes) - use RandomAccessFile.

    I don't think there is any significant difference between them in terms of memory consumption etc. as they are just mediators between JVM and OS.

    0 讨论(0)
  • 2020-12-21 14:39

    If you are only doing sequential access, by themselves they are essentially equivalent; however a DataInputStream around a BufferedInputStream around a FileInputStream will be considerably more efficient than a RandomAccessFile.

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