convert jpg file into byte file using java

I have a jpg image file, I want to convert it into byte file using java program, and what file extension i may use that. Because iReports requires, only byte file to show a static image in the report. such as a background image.
jayaprakashkgAsked:
Who is Participating?
 
ProgrammingSmurfConnect With a Mentor Commented:
sorry ... i cut and paste twice by mistake ,,,, code snippet should be ,.,.,.
package uk.allianz.test.kmg.dirlist;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class ReadJPG {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
		try {
			FileInputStream fi = new FileInputStream("C:\\image.jpg");
			byte[] imageAsBytes = new byte[fi.available()];
			fi.read(imageAsBytes);
			
		} catch (FileNotFoundException ex) {
			
		} catch (IOException ex) {
			
		}
	}
 
}

Open in new window

0
 
ProgrammingSmurfCommented:
not sure what you mean by Byte file?  You can read it into a byte array with something like this ...


package com.example.ExpertsExchange
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class ReadJPG {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	// TODO Auto-generated method stub
 
	try {
		FileInputStream fi = new FileInputStream("C:\\image.jpg");
		byte[] imageAsBytes = new byte[fi.available()];
		fi.read(imageAsBytes);
			
	} catch (FileNotFoundException ex) {
		// error processingpackage uk.allianz.test.kmg.dirlist;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class ReadJPG {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
		try {
			FileInputStream fi = new FileInputStream("C:\\image.jpg");
			byte[] imageAsBytes = new byte[fi.available()];
			fi.read(imageAsBytes);
			
		} catch (FileNotFoundException ex) {
			// error processing
		} catch (IOException ex) {
			// error pro
		}
	}
 
}	

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.