How do I convert a File into an array of bytes?

Discussion in 'Java' started by MattC, Nov 7, 2005.

  1. MattC

    MattC Guest

    I have a File object that represents a file on the file system. I want
    to use that File to obtain a an array of bytes (byte[]). Can someone
    tell me how to do this?

    Thanks
     
    MattC, Nov 7, 2005
    #1
    1. Advertising

  2. MattC

    Roedy Green Guest

    On 7 Nov 2005 08:44:24 -0800, "MattC" <>
    wrote, quoted or indirectly quoted someone who said :

    >I have a File object that represents a file on the file system. I want
    >to use that File to obtain a an array of bytes (byte[]). Can someone
    >tell me how to do this?


    See http://mindprod.com/applets/fileio.html

    tell it you want to read raw bytes, unbuffered from a file.

    Then read the entire file into a byte array File.length() bytes long
    in one fell swoop.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 7, 2005
    #2
    1. Advertising

  3. MattC

    Viv Guest

    Re: How do I convert a File into an array of bytes?

    package Practice;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;


    public class Temp {

    public static void main(String[] args)
    {
    File file = new File("c:/EventItemBroker.java");

    byte[] b = new byte[(int) file.length()];
    try {
    FileInputStream fileInputStream = new FileInputStream(file);
    fileInputStream.read(b);
    for (int i = 0; i < b.length; i++) {
    System.out.print((char)b);
    }
    } catch (FileNotFoundException e) {
    System.out.println("File Not Found.");
    e.printStackTrace();
    }
    catch (IOException e1)
    {
    System.out.println("Error Reading The File.");
    e1.printStackTrace();
    }

    }
    }
     
    Viv, Nov 8, 2005
    #3
  4. MattC

    vrandesh

    Joined:
    May 13, 2010
    Messages:
    1

    Hello,

    I have tested the above code and It is Working absolutely fine.

    However, When I try to stream a PDF file from my File system, The program works fine, But the output seems to be encoded and Data needed cannot be read.

    Is it the File format, i.e. PDF, that makes this code to read the file the way it is?

    How do i just read the contents of the PDF file without having to display all the other configuration data of the pdf file.. ?

    Regards,
    Vrandesh
     
    vrandesh, May 13, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Baptiste PERIN

    how to convert 4 bytes into a float ?

    Jean-Baptiste PERIN, Feb 8, 2005, in forum: Python
    Replies:
    4
    Views:
    883
    Fredrik Lundh
    Feb 8, 2005
  2. Replies:
    5
    Views:
    550
    Flash Gordon
    Apr 9, 2006
  3. Yandos
    Replies:
    12
    Views:
    5,126
    Pete Becker
    Sep 15, 2005
  4. Replies:
    8
    Views:
    506
    Bob Hairgrove
    Apr 10, 2006
  5. Replies:
    5
    Views:
    1,245
    Steven Simpson
    Nov 13, 2007
Loading...

Share This Page