怎样读取这个文件才不会出现乱码呢?
How to read this file so that the characters are not garbled?
我尝试使用 Java.This 文件读取文件没有文件类型。当我用 UltraEdit 文本编辑器打开它时,它看起来像这样: 文件中的第一行是
00 00 10 01 00 51 21 E4 22 0D 6D F1 81 51 21 E2.
我也查了UltraEdit里面的文件编码格式,就是ANSI.But这个00 00 10的文件怎么读取....这样在控制台打印数据?
我在 Java 中有 eclipse 1.7.I 试图在 "GBK"、"GB2312"、"UTF-8" 中读取该文件,但没有 work.When我试着在"ANSI"中读取它,然后这是一个错误,
错误信息
线程中出现异常 "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();
}
}
}
```
我确实在上面问了一个问题,但我会假设你想要执行 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 个字节)。
正如 Stephen 所提到的,实际上没有任何类型的二进制文件编码方案(您提出的类型 - 例如 ANSI 等)。这种编码方案适用于文本文件,并告诉您它是 ANSI、UTF-8 还是 UNICODE 等,并告诉您如何阅读它。
话虽如此,二进制文件确实隐式具有 "encoding scheme"。在二进制文件的情况下,"encoding scheme",正如 Stephen 提到的,是由写入文件的内容决定的。这实际上也适用于文本文件,程序将确定它是否将文本文件写入为 ANSI、UTF-8 或任何编码方案。
对于二进制文件,"encoding scheme" 可能是 JPEG、PNG、GIF、MP3、MP4、ZIP 或 TAR 或数千种其他可能性中的任何一种。同样,这取决于编写文件的程序(例如图像编辑器、音频文件编辑器等)。
希望这能帮助您找到答案。
我尝试使用 Java.This 文件读取文件没有文件类型。当我用 UltraEdit 文本编辑器打开它时,它看起来像这样: 文件中的第一行是
00 00 10 01 00 51 21 E4 22 0D 6D F1 81 51 21 E2.
我也查了UltraEdit里面的文件编码格式,就是ANSI.But这个00 00 10的文件怎么读取....这样在控制台打印数据?
我在 Java 中有 eclipse 1.7.I 试图在 "GBK"、"GB2312"、"UTF-8" 中读取该文件,但没有 work.When我试着在"ANSI"中读取它,然后这是一个错误,
错误信息
线程中出现异常 "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();
}
}
}
```
我确实在上面问了一个问题,但我会假设你想要执行 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 个字节)。
正如 Stephen 所提到的,实际上没有任何类型的二进制文件编码方案(您提出的类型 - 例如 ANSI 等)。这种编码方案适用于文本文件,并告诉您它是 ANSI、UTF-8 还是 UNICODE 等,并告诉您如何阅读它。 话虽如此,二进制文件确实隐式具有 "encoding scheme"。在二进制文件的情况下,"encoding scheme",正如 Stephen 提到的,是由写入文件的内容决定的。这实际上也适用于文本文件,程序将确定它是否将文本文件写入为 ANSI、UTF-8 或任何编码方案。 对于二进制文件,"encoding scheme" 可能是 JPEG、PNG、GIF、MP3、MP4、ZIP 或 TAR 或数千种其他可能性中的任何一种。同样,这取决于编写文件的程序(例如图像编辑器、音频文件编辑器等)。
希望这能帮助您找到答案。