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



Home Java Javafile Java example for Reading file into byte array

Related Tutorials


 
 

Share on Google+Share on Google+

Java example for Reading file into byte array

Advertisement
Java example for Reading file into byte array and links for many tutorials on reading file into byte array.

Java example for Reading file into byte array. You can then process the byte array as per your business needs.

This example shows you how to write a program in Java for reading file into byte array. Sometimes it becomes necessary to read a file into byte array for certain type of business processing. This Java code example explains the process of reading file into byte array.

In this example we will use the java.io.InputStream class which provides the methods for reading from the input stream. The java.io.InputStream class provides the following methods which is useful in reading the file into byte arry.

Methods of java.io.InputStream class:

available()

This method returns the number of bytes which can be read.

close()

This method is used to close the input stream and releases all the system resources associated with it.

mark(int readlimit)

You can use this method to mark the current position in the associated input stream.

markSupported()

This method is used to find if the stream supports the mark and reset methods. So, you can find out if your stream supports mark and reset or not.

read()

This method is used to read the data from input stream into byte array.

read(byte[] b)

This method is used to read bytes from the input stream. It then stores into the buffer array b.

read(byte[] b, int off, int len)

This method is used to read up to len bytes of data. It reads the data from input stream and then stores into an array of bytes.

reset()

This method is used to reset the position to the mark which was set earlier.

skip(long n)

This method is used to skip over the n bytes from the input stream while reading.

Example code of reading file stream into byte array:

In our example are using  java.io.InputStream class discussed above. Here is the complete code of the program:

import java.io.*;

/**
 * This class shows you how to read 
 * file content into byte array
 */

public class ReadFileIntoByteArray 
{
	public static void main(String[] args) 
	{
		System.out.println("Example of Reading file into byte array");
		try{
		//Instantiate the file object
		File file = new File("test.text");
		//Instantiate the input stread
		InputStream insputStream = new FileInputStream(file);
		long length = file.length();
		byte[] bytes = new byte[(int) length];
		int offset = 0, n = 0;
		//Read the data into bytes array
		while (offset < bytes.length
			&& (n = insputStream.read(bytes, offset, bytes.length - offset)) >= 0) {
		  offset += n;
		}
		insputStream.close();
		String s = new String(bytes);
		System.out.println(s);
		}catch(Exception e){
			System.out.println("Error is:" + e.getMessage());
		}
	}
}

Other examples of reading file into byte array:

Read more at Java File - Example and Tutorials.

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: January 18, 2013

Related Tutorials

Discuss: Java example for Reading file into byte array  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com