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