Scenario:
Read the UTF-8-encoded template file into the memory, and then convert it to an HTML file.
Note:
UTF-8 encoded files start with three additional Bytes: 0xef 0xbb 0xbf
A Unicode-encoded file starts with two additional Bytes: 0xff 0xfe (little endian) or 0xfe 0xff (big endian)
No additional bytes for ANSI Encoding
If the read and write operations are improper, the expected results may not be obtained (HTML displays garbled characters)
Read example:
String readfile (string utf8file) {</P> <p> reader streamreader = new inputstreamreader (New fileinputstream (utf8file), "UTF-8 "); </P> <p> reader = new bufferedreader (streamreader); </P> <p> string line = NULL; </P> <p> stringbuilder text = new stringbuilder (); </P> <p> while (line = reader. readline ())! = NULL) {</P> <p> text. append (line); </P> <p >}</P> <p> reader. close (); </P> <p> return text. tostring (); </P> <p >}< br/>
Write example:
String template = readfile ("C: // utf8.template"); </P> <p> string outputfile = "C: // utf8.htm "; <br/> writer = new printwriter (<br/> New outputstreamwriter (New fileoutputstream (outputfile), "UTF-8"); </P> <p> template = template. replace ("$ title $", "UTF-8 read and write"); </P> <p> writer. write (Template); <br/> writer. close (); </P> <p>