0

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();
        }

    }

}
```
XuXing0430
  • 13
  • 3
  • so you want to read from a file and simply print it? – Liam Wilson May 01 '19 at 01:14
  • See this post, should answer your question - https://stackoverflow.com/questions/18556104/read-and-write-text-in-ansi-format Also see: https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html – Adam McClenaghan May 01 '19 at 01:15
  • Is this meant to be text data or is it just a binary file? Presumably binary since that's what your editor shows. Encoding is only applicable for text data. – bgfvdu3w May 01 '19 at 01:17
  • The file is just a binary file.I want to read from a file and simply print it.I change the type in "Cp1232" but still not worked. – XuXing0430 May 01 '19 at 10:20
  • If it is really a binary file, then you won't be print it unless you understand what wrote it. Changing the encoding type won't help. – Stephen C May 02 '19 at 02:44
  • Given the input file is a binary file *and* assuming that the sample shown is a hex dump of the file (i.e. the first byte is 00, the second byte is 00, the third byte is 10 etc), what *exactly* are you expecting the program to output? – GMc May 02 '19 at 02:54

1 Answers1

0

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.

GMc
  • 1,764
  • 1
  • 8
  • 26
  • I tried the code and it really worked,thanks a lot.But still have some problems,some dump data is wrong.especially the data that should be displayed as high position(80-FF).How to figure it out?And i also found that many wrong data shown as FD. – XuXing0430 May 03 '19 at 09:32
  • Can you update your question with the new information? Please show what your inputs are, what you are getting out and what you are expecting to see. – GMc May 03 '19 at 10:08
  • In the meantime, I have played with it a bit and revised my answer to use a DataInputStream - rather than a InputStreamReader. This seems to give a better result. Can you try that? – GMc May 03 '19 at 10:24
  • Normally you should close this question (accept the answer and upvote), then raise a new question. If you keep adding to a question, it get's too broad and isn't as helpful for others as there are too many problems contained in it. Feel free to link back to this question in your new post – GMc May 05 '19 at 00:57
  • There is my new question.Can you help me?https://stackoverflow.com/questions/55988540/how-to-find-a-specific-byte-in-many-bytes – XuXing0430 May 06 '19 at 01:06
  • @XuXing0430 if this has helped you, could you please accept the answer (click on the grey check mark next to the answer) and upvote it? Thanks. – GMc May 13 '19 at 11:03