Java reads Chinese files
Garbled characters often occur when reading Chinese files in Java because of Java coding problems. The following is a simple class for reading Chinese.
When using inputstream to read data, it is best to read multiple bytes at a time. This Io operation improves the speed.
Package Wh;
Import java. Io. bufferedreader;
Import java. Io. bytearrayoutputstream;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filereader;
Import java. Io. filterinputstream;
Import java. Io. inputstream;
Public class stream
{
Public static void main (string [] ARGs)
{
Try
{
Inputstream r = new fileinputstream ("C:/a.txt ");
Bytearrayoutputstream byteout = new bytearrayoutputstream ();
Byte TMP [] = new byte [256];
Byte context [];
Int I = 0;
While (I = R. Read (TMP ))! =-1)
{
Byteout. Write (TMP );
}
Context = byteout. tobytearray ();
String STR = new string (context, "gb2312 ");
// Separate rows
String stra [] = Str. Split ("/N ");
For (INT n = 0; n <stra. length; n ++)
{
System. Out. println (stra [N] + "++ ");
}
// System. Out. println (STR );
} Catch (exception E)
{
// Todo: handle exception
}
}
}