2

I need to do some basic operations on image file in java . My requirements are like :
- opening a file. - read bytes in some order. - write the updated byte at the particular offset - seeking at some offset in file.

Files can be of any size like 2 GB image files.

I want to know, which class in java can provide me the flexibility to do all these operations with ease and with performance efficiency, considering IO in java is slow.

Currently I am considering FileChannel, but I dont know about its performance with files of larger size like in GB. Also it use to read file bytes in ByteBuffer, but if file is large enough , is it appropriate to read all the bytes at same time or should read in chunks. If I read data in chunks, what is the proper size of a chunk?

Please guide me.

Thanks

aga
  • 359
  • 2
  • 4
  • 15

2 Answers2

1

You probably should use RandomAccessFile, it supports seek by position and write. Also I don't think it is accurate to describe IO in Java as slow, often you can achieve C like performance if you use java.io properly.

hgrey
  • 3,033
  • 17
  • 21
0

You can use Java 8 Stream features

Stream<String> lines = Files.lines(Paths.get(args[1]));

But, If you are not using Java-8. You use java.nio package

If I read data in chunks, what is the proper size of a chunk?

You can uses a buffer size of only 1 KB.

mubeen
  • 813
  • 2
  • 18
  • 39