简体   繁体   中英

Most efficient way to write to a file?

I am writing my own image compression program in Java, I have entropy encoded data stored in multiple arrays which I need to write to file. I am aware of different ways to write to file but I would like to know what needs to be taken into account when trying to use the least possible amount of storage. For example, what character set should I use (I just need to write positive and negative numbers), would I be able to write less than 1 byte to a file, should I be using Scanners/BufferedWriters etc. Thanks in advance, I can provide more information if needed.

Read the Java tutorial about IO .

You should

  • not use Writers and character sets, since you want to write binary data
  • use a buffered stream to avoid too many native calls and make the write fast
  • not use Scanners, as they're used to read data, and not write data

And no, you won't be able to write less than a byte in a file. The byte is the smallest amount of information that can be stored in a file.

what character set should I use

You would need to write your data as bytes, not chars, so forget about character set.

would I be able to write less than 1 byte to a file

No, this would not be possible. But to follow decoder expected bit stream you might need to construct a byte, from something like 5 and 3 bits before writing that byte to the file.

Compression is almost always more expensive than file IO. You shouldn't worry about the speed of your writes unless you know it's a bottle neck.

I am writing my own image compression program in Java, I have entropy encoded data stored in multiple arrays which I need to write to file. I am aware of different ways to write to file but I would like to know what needs to be taken into account when trying to use the least possible amount of storage.

Write the data in a binary format and it will be the smallest. This is why almost all image formats use binary.

For example, what character set should I use (I just need to write positive and negative numbers),

Character encoding is for encoding characters ie text. You don't use these in binary formats generally (unless they contain some text which you are unlikely to do initially).

would I be able to write less than 1 byte to a file,

Technically you can use less than the block size on disk eg 512 bytes or 4 KB. You can write any amount less than this but it doesn't use less space, nor would it really matter if it did because the amount of disk is too small to worry about.

should I be using Scanners/BufferedWriters etc.

No, These are for text,

Instead use DataOutputStream and DataInputStream as these are for binary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM