Antani
Antani

Reputation: 133

Writing a big text file fast

How can I write a big text file very fast on Android?
I made some tests using a PrintWriter, a BufferedWriter and a FileWriter, but there are no significative time difference, and writing takes about three times the time of reading.

Upvotes: 0

Views: 542

Answers (4)

elmorabea
elmorabea

Reputation: 3263

Give Okio a spin. Not sure if it's actually faster though.

Upvotes: 0

Denysole
Denysole

Reputation: 4051

For faster file read/write take a look at java.nio package.

This is a good article about java NIO, here you can find comparison with IO package

Upvotes: 0

ezaquarii
ezaquarii

Reputation: 1916

Each memory has a specific block and page size. Due to how writes are done on Flash memories, it matters if you write it in small chunks.

Here is a benchmark with dd run in adb shell:

sailfish:/storage/emulated/0 $ dd if=/dev/zero of=test bs=4k count=$((32*1024))                                                                                                                                                                                              
32768+0 records in
32768+0 records out
134217728 bytes transferred in 0.521 secs (257615600 bytes/sec)

Another one for 1k block:

sailfish:/storage/emulated/0 $ dd if=/dev/zero of=test bs=1k count=$((128*1024))                                                                                                                                                                                           
131072+0 records in
131072+0 records out
134217728 bytes transferred in 1.369 secs (98040707 bytes/sec)

Writing data in small chunks makes it almost 3x slower. I encourage you to benchmark your memory to see if you hit a hardware limitation. Experiment with write buffer sizes. If your memory is genuinely slower during writes, there is nothing more you can do.

Upvotes: 0

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28229

Android, just like every single other device with a storage unit, has limited writing speed.

The only thing you can do to speed up file writing and reading is get a better storage unit (which isn't as easy on an Android device as you can't just screw it open and replace the unit).

So when it comes to file reading and writing, you're limited by hardware.

Upvotes: 1

Related Questions