public class Demo03 {
public static void main(String[] args) throws IOException{
get1("G:\JavaFile\讀取二.txt");
}
//以utf-8的格式讀取文件
public static char[] get1(File fi)throws IOException{
char[] ch=null;
BufferedReader reader=new BufferedReader(
new InputStreamReader(
new FileInputStream(fi)," utf-8")
);
CharArrayWriter writer=new CharArrayWriter();
char[] ch01=new char[2];
int len=0;
while((len=reader.read(ch01))!=-1)
{
writer.write(ch01, 0, len);
}
writer.flush();
ch=writer.toCharArray();
System.out.println(new String(ch));
reader.close();
return ch;
}
public static char[] get1(String str)throws IOException{
File fi=new File(str);
return get1(fi);
}
.txt文檔的編碼格式是ANSI的,你要把它改成utf-8才可以,你的程序中new FileInputStream(fi)," utf-8"),字符編碼格式裡多了一個空格。