Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Reading a File into a Byte Array

 
 

Share on Google+Share on Google+

Reading a File into a Byte Array

Advertisement
In this program we will read a file in the form of a byte array. We can do it very easily.

Reading a File into a Byte Array

     

In this program we will read a file in the form of a byte array. We can do it very easily. Firstly we will create an object of File class and pass the name of the file which we want to read, in the constructor of File class. We will use DataInputStream to read primitive data types.

We are using the following methods:

length(): It gives the length of the file.

Getting the file size code: ReadInByteArray.java

import java.io.*;

public class ReadInByteArray 
{
public static void main(String[] args
 {
 try
 {
 File file = new File("tapan.txt")
int size = (int)file.length();//It gives the length of the file. 
 
 System.out.println("Size of the file: " + size);
   }
  catch (Exception e)
  {
  System.out.println("Exception has been thrown :" + e);
  }
  }
}

Download this program

The following code helps you in getting the file size in byte array.

 Here is the Byte Array code: ByteArrayExample.java

import java.io.*;

public class ByteArrayExample{
public static void main(String[] args) {
  try{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter File name: ");
  String str = bf.readLine();
  File file = new File(str)
  //File length
  int size = (int)file.length()
  if (size > Integer.MAX_VALUE){
  System.out.println("File is to larger");
  }
  byte[] bytes = new byte[size]
DataInputStream dis = new DataInputStream(new FileInputStream(file))
  int read = 0;
  int numRead = 0;
  while (read < bytes.length && (numRead=dis.read(bytes, read,
  bytes.length-read
)) >= 0) {
  read = read + numRead;
  }
  System.out.println("File size: " + read);
  // Ensure all the bytes have been read in
  if (read < bytes.length) {
System.out.println("Could not completely read: "+file.getName());
  }
  }
  catch (Exception e){
  e.getMessage();
  }
  }
}
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 11, 2008

Related Tutorials

Discuss: Reading a File into a Byte Array   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Duy Phong Nguyen
November 3, 2011
Good

I have problem about this. And when I read this topic, my problem has been resolved Thank you so much !!!
Jon
January 23, 2012
lonf filesize

i am using the command long filesize = file.length() but this is returning the wrong value. it is saying there are 0 bytes in a folder that has at least 4 GB in?
Brian S.
August 1, 2012
Error in code

//File length int size = (int)file.length(); if (size > Integer.MAX_VALUE){ System.out.println("File is to larger"); } The size check is redundant. By casting the file size as an int, its value will never exceed Integer.MAX_VALUE, because an overflow will occur on the first line. You should declare size as long and not cast length()
DMCA.com