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



Home Java Java-get-example Get Byte Array from File

Related Tutorials


 
 

Share on Google+Share on Google+

Get Byte Array from File

Advertisement
In this section, we are going to show the array of bytes from the specified file. For this, a file 'Hello.txt' is passed into the constructor of class File.

Get Byte Array from File

     

In this section, we are going to show the array of bytes from the specified file. For this, a file 'Hello.txt' is passed into the constructor of class File.

 fileInputStream.read(b)- This method reads the the file in bytes.

int b[i]- This will show the character in bytes.

char b[i]- This will show the character.

 

Here is the code of GetByteArrayFromfile.java

import java.io.*;

public class GetByteArrayFromfile {
  public static void main(String[] args) throws Exception
{
File file = new File("Hello.txt");

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.println(
 "b[" + i + "] = " +((int)b[i] < 11  "  " "") +
  b[i] + " , " +" character=(" + (char)b[i] + ")");
  }
catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
}
}

Output will be displayed as:

 

Download Source Code

 

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: October 23, 2008

Related Tutorials

Discuss: Get Byte Array from File  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Boo
December 15, 2011
Sucks

This is not a byte array, this is just the file's name separated into a byte array. Useless piece of summer loving pie!
DMCA.com