Java reads Chinese files

Source: Internet
Author: User
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
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.