标签:substr 转换 except rac argument line exec als ==
摘自:https://www.oschina.net/code/snippet_87799_1612
Native2Ascii文件转换 -- 待完善
1 package com.xxx.xxx.Util; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.io.InputStreamReader; 10 import java.io.OutputStreamWriter; 11 12 /** 13 * 类、接口等说明 14 * 15 * @Date 16 * @version v1.0 17 * @author 18 */ 19 public class ConvertFileNative2ASCIIUtil { 20 21 public static boolean native2Ascii(String srcFileName, String srcFilecharsetName, String dstFileName, 22 String dstFilecharsetName) throws IOException { 23 24 File srcFileNameFile = new File(srcFileName); 25 26 if (!srcFileNameFile.exists()) { 27 throw new IOException(" File(" + srcFileName + ") Parameter does not reference a file that exists"); 28 } else if (!srcFileNameFile.isFile()) { 29 throw new IOException("File(" + srcFileName + ") Parameter exists, but does not reference a file"); 30 } else if (!srcFileNameFile.canRead()) { 31 throw new IOException("File(" + srcFileName + ") exists and is a file, but cannot be read."); 32 } else { 33 String content = readFile(srcFileName, srcFilecharsetName); 34 String ascii = Native2ASCIIUtil.native2Ascii(content); 35 writeFile(dstFileName, ascii, dstFilecharsetName); 36 } 37 38 return true; 39 } 40 41 public static boolean ascii2Native(String srcFileName, String srcFilecharsetName, String dstFileName, 42 String dstFilecharsetName) throws IOException { 43 44 File srcFileNameFile = new File(srcFileName); 45 46 if (!srcFileNameFile.exists()) { 47 throw new IOException(" File(" + srcFileName + ") Parameter does not reference a file that exists"); 48 } else if (!srcFileNameFile.isFile()) { 49 throw new IOException("File(" + srcFileName + ") Parameter exists, but does not reference a file"); 50 } else if (!srcFileNameFile.canRead()) { 51 throw new IOException("File(" + srcFileName + ") exists and is a file, but cannot be read."); 52 } else { 53 String content = readFile(srcFileName, srcFilecharsetName); 54 String nativeString = Native2ASCIIUtil.ascii2Native(content); 55 writeFile(dstFileName, nativeString, dstFilecharsetName); 56 } 57 58 return true; 59 } 60 61 public static void writeFile(String path, String content, String encoding) throws IOException { 62 File file = new File(path); 63 file.delete(); 64 file.createNewFile(); 65 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)); 66 writer.write(content); 67 writer.close(); 68 } 69 70 public static String readFile(String path, String encoding) throws IOException { 71 String content = ""; 72 File file = new File(path); 73 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)); 74 String line = null; 75 while ((line = reader.readLine()) != null) { 76 content += line + "\n"; 77 } 78 reader.close(); 79 return content; 80 } 81 }
1 package com.xxx.xxx.Util; 2 3 /** 4 * 类、接口等说明 5 * 6 * @Date 7 * @version v1.0 8 * @author 9 */ 10 public class Native2ASCIIUtil { 11 12 /** 13 * prefix of ascii string of native character 14 */ 15 private static String PREFIX = "\\u"; 16 17 /** 18 * Native to ascii string. It‘s same as execut native2ascii.exe. 19 * @param str native string 20 * @return ascii string 21 */ 22 public static String native2Ascii(String str) { 23 char[] chars = str.toCharArray(); 24 StringBuilder sb = new StringBuilder(); 25 for (int i = 0; i < chars.length; i++) { 26 sb.append(char2Ascii(chars[i])); 27 } 28 return sb.toString(); 29 } 30 31 /** 32 * Native character to ascii string. 33 * @param c native character 34 * @return ascii string 35 */ 36 private static String char2Ascii(char c) { 37 if (c > 255) { 38 StringBuilder sb = new StringBuilder(); 39 sb.append(PREFIX); 40 int code = (c >> 8); 41 String tmp = Integer.toHexString(code); 42 if (tmp.length() == 1) { 43 sb.append("0"); 44 } 45 sb.append(tmp); 46 code = (c & 0xFF); 47 tmp = Integer.toHexString(code); 48 if (tmp.length() == 1) { 49 sb.append("0"); 50 } 51 sb.append(tmp); 52 return sb.toString(); 53 } else { 54 return Character.toString(c); 55 } 56 } 57 58 /** 59 * Ascii to native string. It‘s same as execut native2ascii.exe -reverse. 60 * @param str ascii string 61 * @return native string 62 */ 63 public static String ascii2Native(String str) { 64 StringBuilder sb = new StringBuilder(); 65 int begin = 0; 66 int index = str.indexOf(PREFIX); 67 while (index != -1) { 68 sb.append(str.substring(begin, index)); 69 sb.append(ascii2Char(str.substring(index, index + 6))); 70 begin = index + 6; 71 index = str.indexOf(PREFIX, begin); 72 } 73 sb.append(str.substring(begin)); 74 return sb.toString(); 75 } 76 77 /** 78 * Ascii to native character. 79 * @param str ascii string 80 * @return native character 81 */ 82 private static char ascii2Char(String str) { 83 if (str.length() != 6) { 84 throw new IllegalArgumentException("Ascii string of a native character must be 6 character."); 85 } 86 if (!PREFIX.equals(str.substring(0, 2))) { 87 throw new IllegalArgumentException("Ascii string of a native character must start with \"\\u\"."); 88 } 89 String tmp = str.substring(2, 4); 90 int code = Integer.parseInt(tmp, 16) << 8; 91 tmp = str.substring(4, 6); 92 code += Integer.parseInt(tmp, 16); 93 return (char) code; 94 } 95 }
标签:substr 转换 except rac argument line exec als ==
原文地址:https://www.cnblogs.com/LiuYanYGZ/p/9626670.html