亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讀取這個文件使字符不亂碼?

如何讀取這個文件使字符不亂碼?

MMMHUHU 2022-11-02 16:53:47
我嘗試使用 Java 讀取文件。該文件沒有文件類型。當我使用 UltraEdit 文本編輯器打開它時,它看起來像這樣:文件中的第一行是00 00 10 01 00 51 21 E4 22 0D 6D F1 81 51 21 E2。我還檢查了 UltraEdit 中的文件編碼格式,它是 ANSI。但是如何在 00 00 10....中讀取這個文件并在控制臺上打印數據?我在 Java 1.7 中有 Eclipse。我試圖在“GBK”、“GB2312”、“UTF-8”中讀取該文件,但沒有工作。當我試圖在“ANSI”中讀取它時,這是一個錯誤,錯誤信息線程“主”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();        }    }}```
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

我確實在上面問了一個問題,但我假設您想要執行 HexDump,請考慮以下程序:


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

        }

    }    

}

它將獲取樣本文件并將各個字節轉儲為十六進制值(每行 16 個字節)。


正如斯蒂芬所提到的,對于二進制文件,實際上并沒有任何類型的編碼方案(您建議的類型 - 例如 ANSI 等)。這種類型的編碼方案適用于文本文件,并告訴您它是 ANSI、UTF-8 還是 UNICODE 等,并告訴您如何閱讀它。話雖如此,二進制文件確實隱含著“編碼方案”。在二進制文件的情況下,正如斯蒂芬提到的,“編碼方案”由文件的寫入內容決定。這實際上也適用于文本文件,程序將確定它是否將文本文件寫入為 ANSI、UTF-8 或任何編碼方案。對于二進制文件,“編碼方案”可能是 JPEG、PNG、GIF、MP3 或 MP4、ZIP 或 TAR 或數千種其他可能性中的任何一種。同樣,這是由編寫文件的程序決定的(例如


我希望這可以幫助您找到答案。


查看完整回答
反對 回復 2022-11-02
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號