先,准备一个TXT文件,带生僻字的
写一个java程序读它import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;public class Test
{ public static void main(String[] args) throws IOException
{ String s=read(new File("C:/test.txt"),"UTF-8");
System.out.println(s);
}
public static String read(File file,String charset) throws IOException
{ InputStream fis=new FileInputStream(file);
byte[] bytes=new byte[fis.available()];
fis.read(bytes,0,bytes.length);
fis.close();
String s=new String(bytes,charset);
return s;
}
}
结果显示为?
如何解决呀
写一个java程序读它import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;public class Test
{ public static void main(String[] args) throws IOException
{ String s=read(new File("C:/test.txt"),"UTF-8");
System.out.println(s);
}
public static String read(File file,String charset) throws IOException
{ InputStream fis=new FileInputStream(file);
byte[] bytes=new byte[fis.available()];
fis.read(bytes,0,bytes.length);
fis.close();
String s=new String(bytes,charset);
return s;
}
}
结果显示为?
如何解决呀
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;import java.nio.charset.Charset;
import java.nio.ByteBuffer;
public class Test
{ public static void main(String[] args) throws IOException
{ String s=read(new File("test.txt"));
System.out.println(s);
}
public static String read(File file) throws IOException
{ InputStream fis=new FileInputStream(file);
byte[] bytes=new byte[fis.available()];
fis.read(bytes,0,bytes.length);
fis.close();
ByteBuffer buf = ByteBuffer.wrap(bytes);
Charset c = Charset.defaultCharset();
String result = c.decode(buf).toString();
return result;
}
}
即,該生僻字佔用2个char了,普通漢字只佔用1个
如果看byte數,UTF8下,該生僻字佔用4个byte,普通漢字只佔用3个這樣在讀文本時,會對程序造成一定困擾。有沒有什麼類似String的Class,將普通漢字與生僻漢字一視同仁的嗎?