Hi,
Foll is my code. Here I am reading bytes of data,specifying that it is in cp420 (EBCIDC Arabic) format and then writing to the o/p file in UTF-8 format.
However, there seems to be some problem.There are some junk characters getting written to the file esp the one's where the hex value is alphanumeric for ex 8D,8C etc. If the hex value is numeric then the o/p is correct.
What am I doing wrong in the code.
Also I need to insert a carriage return after every bytes of data read.
Plz help me guys
Nikhil
import java.io.*;
public class ReadBinaryData {
public static void main(
String args[]){
try{
File file = new File("D:\\MYDATA.DATASETS");
InputStream is = new FileInputStream(file);
File outfile = new File( "D:\\testHexFile.txt" );
FileOutputStream fout = new FileOutputStream( outfile);
String s = null;
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File is too large");
System.exit(0);
}
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
s = new String( bytes, "cp420" );
byte[] output = s.getBytes( "UTF-8" );
fout.write(output);
fout.close();
// return bytes;
}catch(Exception e){
System.out.println("Exception e"+e.toString());
}
}// End of main
}//End of class