Results 1 to 6 of 6

Thread: Store content of file in array of bytes

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Store content of file in array of bytes

    Hello,
    I would like to get an array of byte [] from a file (File Object). So, I there fore initially used a file object
    File file = new File (path)
    and it would supply an array byte [] from the contents of File. Is that possible? That is I want to store content of the file in an array of the database.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Store content of file in array of bytes

    Hi,
    It is very easy with Streams, you can do it in this way
    FileInputStream in = new FileInputStream(ObjFile);
    int next = in.read()
    byte b;
    b = (byte)next;
    And that's how you recover your byte. To make a table hop a small loop and it will work. the method read () returns -1 when you reach the end of the file.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Store content of file in array of bytes

    Hello,
    You can optimize if you have a large file to read, rather than read by byte, instead use the method read (byte [] b) you can use FileInputStream. You can read and ask directly, such as some bytes in a byte array, and make a loop to reach the end of the file, storing the previous table, a Vector or ListArray, for example, have a dynamic structure. Be careful though if you have large files to read, has not had an OutOfMemory. It returns -1 in the same way when there's nothing to read, and it lets you read your files very quickly.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Store content of file in array of bytes

    Hello,
    Here is a simple example to convert an entire file into an array of byte
    Code:
    File fl = new File(path);
    byte[] tb = new byte[fl.length()];
    FileInputStream in = new FileInputStream(fl);
    in.read(board);
    Hope this program will help you.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Store content of file in array of bytes

    Hi,
    I agree tho this, unless and until you have a very heavy file. I can not see spending a few hundred MB in loading of a sudden. Otherwise, to answer your question, you read arrays of bytes to some bytes for example, that you fill in a Vector, without going into detail, with the exception handling and other.

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Store content of file in array of bytes

    Hello,
    I completely agree with the above, just have a look at the below example.
    Code:
    byte [] currdt = new byte[500];
    numchrd = myReader.read(currdt,0,500);
    StoredBytes.add(currdt);
    Here, you read in blocks of 500 bytes through AC in a loop. On large files, you can not load all of a sudden.

Similar Threads

  1. SanDisk flash drive “0 bytes used and 0 bytes free”
    By Coloma in forum Hardware Peripherals
    Replies: 5
    Last Post: 03-12-2012, 10:56 AM
  2. Convert IP address to array of bytes
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 11:54 AM
  3. Bytes not written to a file
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 01:17 PM
  4. How to store space in a array in a C program
    By NetworkeR in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 12:41 PM
  5. How to store byte array to database with JDBC?
    By RogerFielden in forum Software Development
    Replies: 3
    Last Post: 23-09-2009, 10:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,983,833.11873 seconds with 17 queries