Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Reading and Writing Binary Files

 
Ranch Hand
Posts: 126
1
Monad Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the best strategy for reading and writing binary files of unknown size in Java? I know in languages like C/C++ you prompt the OS for the page-size and use that for efficient binary reads and writes but I'm assuming that's not available in Java or that it will make the program non-portable.

So is there an efficient array size for reading and writing binary files in Java?
 
Saloon Keeper
Posts: 12419
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it REALLY is binary data and not text, you wrap a BufferedInputStream around Files.newInputStream() and depend on the default buffer size.

If it's textual data, you just call Files.newBufferedReader() and be done with it.
 
Gerard Gauthier
Ranch Hand
Posts: 126
1
Monad Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my case I'm sending data from a PDF file.

Here's an example I found

http://www.avajava.com/tutorials/lessons/how-do-i-serve-up-a-pdf-from-a-servlet.html

Is the above correct for a PDF file? Is it correct for a binary file?
 
Marshal
Posts: 25946
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gerard Gauthier wrote:Is the above correct for a PDF file? Is it correct for a binary file?



Well, a PDF file is a binary file. And yes, it is correct. At least, I think it is.

However it copies one byte at a time. Some people might be concerned about that and wonder if using a buffer of larger than one byte would be better -- especially since PDF files of megabyte size aren't uncommon these days.

But my feeling is that Files.copy(Path, OutputStream) is the best way to do it. Why? Because you don't have to write any code which might accidentally leave off the last few bytes or something, and you don't have to download code from possibly dodgy tutorials.

And I notice that the document for that Files.copy method mentions that you should flush the output stream if you can -- the tutorial code doesn't do that, but maybe it doesn't matter.
 
Rancher
Posts: 214
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done a lot of things that read binary files.

You may be interested in the following open source project I made: https://github.com/zach-cloud/BinaryReader

Maven import:


Create a BinaryReader from your file:


Methods are provided to read a generic number of bytes, read null terminated strings, read string of length, read integers, etc.
Or just read the code to see how I did it (it's simple)
 
Gerard Gauthier
Ranch Hand
Posts: 126
1
Monad Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul, I went with Files.copy(Path, OutputStream). And yes I agree that the library method should handle all the details of a simple operation like move all these bytes from here to there..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic