I tried to read a file using Java.This file do not have a file type. When I use UltraEdit text editor to open it,it looks like this: The first line in file is
00 00 10 01 00 51 21 E4 22 0D 6D F1 81 51 21 E2.
I also checked the File encoding format in UltraEdit, it's ANSI.But how to read this file in 00 00 10....this way and print data on the Console?
I have eclipse in Java 1.7.I tried to read that file in "GBK","GB2312","UTF-8",but did not work.When I tried to read it in "ANSI",then this is a error,
Error message
Exception in thread "main" java.io.UnsupportedEncodingException: ANSI.
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Deconde{
public static void main (String []args) throws Exception{
//File byte stream
FileInputStream fis=new FileInputStream("D:\\0testData\\Data_21");
//A bridge of byte streams and character streams that can specify a specified character format
InputStreamReader isr=new InputStreamReader(fis,"ANSI");
String str=null;
int c=0;
while((c=isr.read())!=-1)
System.out.print((char)c);
System.out.println("_______________________________________________");
//Read characters directly, as long as the encoding problem is ok
BufferedReader br=new BufferedReader(isr);
str=br.readLine();
while(str!=null)
{
System.out.println(str);
str=br.readLine();
}
System.out.println("______________________________________________________");
//Use the default encoding of the InputStreamReader, no problem when it is ANSI
BufferedReader br2=new BufferedReader(new InputStreamReader(fis));
str=br2.readLine();
while(str!=null)
{
System.out.println(str);
str=br2.readLine();
}
}
}
```
I did ask a question above, but I will assume that you are wanting to perform a HexDump, consider the following program:
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
public class HexDump {
public static void main(String[] args) {
try {
InputStream isr = new DataInputStream(new FileInputStream("C:\\Temp\\some_file.dat"));
int bytesPerLine = 16;
int byteCount = 0;
int data;
while ((data = isr.read()) != -1) {
if (byteCount == 0)
System.out.println();
else if (byteCount % bytesPerLine == 0)
System.out.printf("\n", byteCount);
else
System.out.print(" ");
System.out.printf("%02x", data & 0xff);
byteCount += 1;
}
System.out.println();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
It will take the sample file and dump the individual bytes as hexadecimal values (16 bytes per line).
As Stephen mentioned, there isn't really any sort of an encoding scheme (of the type you are proposing - e.g. ANSI etc) for binary files. That type of encoding scheme applies to text files and tells you whether it is ANSI, or UTF-8, or UNICODE etc and tells you how to read it. Having said that, binary files do implicitly have an "encoding scheme". In the case of binary files, the "encoding scheme", as Stephen mentioned, is determined by what wrote the file. This is actually true also for text files, the program will determine if it is writing the text file as ANSI, UTF-8 or whatever encoding scheme. For binary files, the "encoding scheme" might be JPEG, or PNG, or GIF, or MP3 or MP4 or ZIP or TAR or any one of thousands of other possibilities. Again, this is determined by the program that wrote the file (e.g. an image editor, an audio file editor etc).
I hope this helps you find your answer.