我正在尝试读取实际上是 UTF-8 编码文本文件的文件内容。当我尝试通读 InputStreamReader 时,它会返回给我,无法识别的字符会在内容的开头追加。有人可以帮帮我吗?
package com.test.read;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class UTFReader {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File fi= new File(
"E:\\Enterprise\\workspace\\eclipse_workspace\\tocf\\r13_27-8-2014\\ReadUTF9\\src\\com\\test\\read\\TESTUTF.txt");
System.out.println("Default Encoding in system :" +System.getProperty("file.encoding"));
FileInputStream fis1=new FileInputStream(fi);
FileInputStream fis2=new FileInputStream(fi);
FileInputStream fis3=new FileInputStream(fi);
System.out.println("Reading the UTF emcoded File..");
InputStreamReader isr1=new InputStreamReader(fis1, "ISO-8859-1");
InputStreamReader isr2=new InputStreamReader(fis2, "UTF-8");
InputStreamReader isr3=new InputStreamReader(fis3, "Cp1252");
System.out.println("Reading streams with different charset...");
BufferedReader in1 = new BufferedReader(isr1);
BufferedReader in2 = new BufferedReader(isr2);
BufferedReader in3 = new BufferedReader(isr3);
System.out.println("ISO-8859-1 Stream Encoding : "+isr1.getEncoding() );
System.out.println("UTF-8 Stream Encoding : "+isr2.getEncoding() );
System.out.println("Cp1252 Stream Encoding : "+isr3.getEncoding() );
String str1 = in1.readLine();
String str2 = in2.readLine();
String str3 = in3.readLine();
System.out.println("First output of file:"+str1);
System.out.println("Second output of file:"+str1);
System.out.println("Third output of file:"+str1);
}
}
结果:
在eclipse IDE中:
Default Encoding in system :Cp1252
Reading the UTF emcoded File..
Reading streams with different charset...
ISO-8859-1 Stream Encoding : ISO8859_1
UTF-8 Stream Encoding : UTF8
Cp1252 Stream Encoding : Cp1252
First output of file:ENQUIRY.SEELECT
Second output of file:ENQUIRY.SEELECT
Third output of file:ENQUIRY.SEELECT