JAVA Chinese Unicode Mutual conversion file read

Source: Internet
Author: User

Package Com.test;import Org.junit.test;public class Junittest {@Test public void Test () {String path = "D:        \\1.txt ";        String NewPath = "D:\\2.txt"; try {//Prevent file build or read failures, catch errors with catch and print, or throw/* read into TXT file *///to read input from above path.            TXT file filename = new file (path);            Create an input stream object reader InputStreamReader reader = new InputStreamReader (new FileInputStream (filename));            Set up an object that turns the contents of a file into a language that the computer can read BufferedReader br = new BufferedReader (reader);            String line = "";                line = Br.readline (); /* Write TXT file *///relative path, if not, create a new output.            TXT file writename = new file (NewPath);            Create a new file Writename.createnewfile ();            BufferedWriter out = new BufferedWriter (new FileWriter (Writename));                while (line! = null) {//reads one row at a time lines = Br.readline ();              System.out.println (Line.tostring ());  \ r \ n is a newline out.write (cntounicode line + "\ r \ n"); Out.flush (); Press the buffer contents into the file} out.close ();        Finally, remember to close the file} catch (Exception e) {e.printstacktrace ();        }} @Test public void Test1 () {System.out.println (Cntounicode ("Force")); System.out.println (UNICODETOCN ("\\u4e2d\\u56fd\\u4eba\\u6c11\\u89e3\\u653e\\u519b\\u37\\u31\\u39\\u38\\u38\\    u90e8\\u961f ")); }/** * Chinese to Unicode * @param cn * @return */private static string Cntounicode (String cn) {CH        ar[] chars = Cn.tochararray ();        String returnstr = "";        for (int i = 0; i < chars.length; i++) {returnstr + = "\\u" + integer.tostring (Chars[i], 16);    } return RETURNSTR;         }/** * Unicode to Chinese * @param Unicode * @return */private static string Unicodetocn (string Unicode) { The/** is split with \ U, because Java annotations also recognize Unicode, so a space is added to the middle, */string[] STRs = Unicode.split ("\\\\u ");        String returnstr = "";        Because the Unicode string begins with \ U, the first character that is split is "".        for (int i = 1; i < strs.length; i++) {returnstr + = (char) integer.valueof (Strs[i], +). Intvalue ();    } return RETURNSTR; }}

  

JAVA Chinese Unicode Mutual conversion file read

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.