[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


copying bytes from file

Posted on 2006-04-19
Medium Priority
Last Modified: 2010-04-17
hi there
i have a wav file, i wish to copy the file into a buffer of type Byte and then read from the buffer using java, can any one please help us with the solution, thanks
Question by:abbas486
  • 2
LVL 25

Expert Comment

ID: 16492280
As in a byte array, or a ByteBuffer instance?

LVL 25

Accepted Solution

InteractiveMind earned 300 total points
ID: 16492369
One solution is to read the file into a byte [] array, via a FileInputStream.
If you then wanted a ByteBuffer, you just use the wrap() method of the ByteBuffer class to shove it into a ByteBuffer.

Perhaps a more efficient approach would be to use a FileChannel to read the file into a MappedByteBuffer instance, as in:

File file = new File( "somebytes.wav" );
fis = new FileInputStream( file );

FileChannel fc = fis.getChannel();
MappedByteBuffer bb = fc.map( FileChannel.MapMode.READ_ONLY, 0, file.length() ) ;

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Introduction to Processes
Starting up a Project

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question