copying bytes from file

Posted on 2006-04-19
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
    LVL 25

    Expert Comment

    As in a byte array, or a ByteBuffer instance?

    LVL 25

    Accepted Solution

    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 = FileChannel.MapMode.READ_ONLY, 0, file.length() ) ;

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    A short article about a problem I had getting the GPS LocationListener working.
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now