Pretty simple question: what’s the performance difference between a Byte Stream and a Character Stream?
The reason I ask is because I’m implementing level loading from a file, and initially I decided I would just use a Byte Stream for the purpose, because it’s the simplest type, and thus it should perform the best. But then I figured that it might be nice to be able to read and write the level files via a text editor instead of writing a more complex level editor (to start off with). In order for it to be legible by a text editor, I would need to use Character streams instead of Byte streams, so I’m wondering if there’s really any performance difference worth mentioning between the two methods? At the moment it doesn’t really matter much since level loading is infrequent, but I’d be interested to know for future reference, for instances where I might need to load levels from hard drive on the fly (large levels).
I assume you are compare Input/OutputStream with Reader/Writer streams. If that is the case the performance is almost the same. Unless you have a very fast drive, the bottleneck will be almost certainly the disk, in which case it doesn’t matter too much what you do in Java.
All files are actually a stream of bytes. So when you use Reader/Writer it uses an encoder to convert bytes to chars and back again. There is nothing stopping you reading and writing bytes directly which do exactly the same thing.
You wouldn’t, but it might make it easier. If you only want ASCII encoding, there is no difference. If you want UTF-8 encoding with non-ASCII characters using chars is likely to be simpler.
I would worry about correctness first and performance second.
Java can read/write text at about 90 MB/s, most hard drives and networks are not that fast. However if you need to write GBs in second and you have fast SSD, then it might make a difference. SSDs can perform 500 MB/s or more and then I would suggest you use NIO to maximise performance.