Encapsulation for reading Chinese character string files in mobile games

Source: Internet
Author: User
When developing mobile RPG games, there are usually many dialogs. Generally, we store the dialogs in resource files and then read them in the program. Reading Spanish characters is relatively simple. However, reading Chinese characters is a little complicated. The following is a class that supports reading Chinese strings. Note: The resource file must be saved in unicode format (in notepad, choose File> Save As dialog box to select the saved encoding format ). The source code is as follows: Import java. Io. *; import java. util. vector;/*** chineasereader: reads files saved in Unicode encoding format and supports reading Chinese strings. * @ Author floating white clouds **/public class chineasereader {private string Path = "/unicode_cn_string.txt"; private Boolean bereaded = false; // Private inputstream reader; private vector venterposition; // store the position of the line feed (hard press Enter) Private vector vtext; // store the vector/*** of the text of each row to set the path and file name of the file to be read: "path/FILENAME" * @ Param path */Public void setpath (string path) {This. path = path; bereaded = false;}/*** get the string of the specified row, with the row number starting from 1 Start. * @ Param line number of rows * @ return returns the string of the specified row */Public String Getline (INT line) {If (! Bereaded) readfile (); If (line-1> vtext. size () | (line-1) <0) return NULL; Return (string) vtext. elementat (line-1);}/*** gets the string starting from the tag flag in the specified row, and the row number starts from 1. * @ Param line number * @ Param tag (<tag>) * @ return */Public String Getline (INT line, string tag) {If (! Bereaded) readfile (); string ret = new string (); If (line-1> vtext. size () | (line-1) <0) ret = NULL; else {ret = (string) vtext. elementat (line-1); string tagstr = "<" + tag + ">"; int Pos = ret. indexof (tagstr); If (Pos =-1) {system. out. println ("class chinesereader can not read <" + tag + ">. "); ret = NULL;} else {pos = ret. indexof (">"); ret = ret. substring (Pos + 1) ;}} return ret;}/*** read data by row and save the row mail. */Private void readfile () {If (bereaded) return; try {reader = Class. forname ("Java. lang. object "). getresourceasstream (PATH);} catch (exception e) {} venterposition = new vector (0, 1); vtext = new vector (0, 1 ); try {byte [] bytesread = new byte [4096*2]; int readlength = 0; int C1, C2; string stext = NULL; reader. skip (2); // fffe if (readlength = reader. read (bytesread)> 0) {char outputchars [] = new char [readlength/2]; Int J = 0; // cursor in the outputchars array for (INT I = 0; I <readlength & J <readlength/2; I = I + 2, J ++) {C1 = (INT) bytesread [I]; C2 = (INT) bytesread [I + 1]; // line feed: "/N ", decimal hexadecimal // '/'92 5C // 'n' 110 6e if (I + 3 <readlength) & (bytesread [I] = 92) & (bytesread [I + 1] = 0) & (bytesread [I + 2] = 110) & bytesread [I + 3] = 0) {outputchars [J] = '/N'; I = I + 2; continue;} // press enter to wrap the line: "/N ", character in decimal hexadecimal format // press ENTER 13 0d // newline 10 0a if (I + 3 <readlength) & (bytesread [I] = 0x0d) & (bytesread [I + 1] = 0) & (bytesread [I + 2] = 0x0a) & (bytesread [I + 3] = 0 )) {venterposition. addelement (New INTEGER (j);} If (C1 <0) C1 + = 256; If (C2 <0) C2 + = 256; outputchars [J] = (char) (C2 <8 + C1);} stext = new string (outputchars); stext = stext. trim ();} else stext = ""; int num = venterposition. size () + 1; // Add the last text character int startpos = 0, endpos = 0; // start point, and end point for (INT I = 0; I <num-1; I ++) {endpos = (integer) venterposition. elementat (I )). intvalue (); vtext. addelement (stext. substring (startpos, endpos); startpos = endpos;} vtext. addelement (stext. substring (startpos); bereaded = true; // system. out. println (stext); // for (I = 0; I <vtext. size (); I ++) // system. out. println (I + "is" + (string) vtext. elementat (I);} catch (exception e) {bereaded = false; E. printstacktrace ();}}}

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.