java 字符串 转编码_Java下进行字符串的编码转换

1 import java.io.*;2 importjava.nio.charset.Charset;3 importjava.util.Iterator;4 importjava.util.Set;5 importjava.io.UnsupportedEncodingException;6

7 public classConvertor {8 public static final String Old_Charset = "Unicode";9 public static final String ISO_8859_6 = "ISO-8859-6";10 public static final String ISO_8859_7 = "ISO-8859-7";11 public static final String ISO_8859_8 = "ISO-8859-8";12 public static final String TIS_620 = "TIS-620";13

14 public static voidPrintAllSupportedCharset()15 {16 Set charsetNames =Charset.availableCharsets().keySet();17 System.out.println("-----the number of jdk1.67's charset is "+charsetNames.size()+"-----");18 for (Iterator it =charsetNames.iterator(); it.hasNext();)19 {20 String charsetName =(String) it.next();21 System.out.println(charsetName);22 }23 }24

25 public static void PrintBytes(byte[] b)26 {27 for (int i = 0; i < b.length; i++) {28 String hex = Integer.toHexString(b[i] & 0xFF);29 if (hex.length() == 1) {30 hex = '0' +hex;31 }32 System.out.print(hex.toUpperCase() + " ");33 }34 System.out.println();35 }36

37 public static voidConvertCharset(String filePath, String fileName, String destDir, String oldCharset, String newCharset)38 {39 try

40 {41 System.out.println(filePath);42 InputStream in = newFileInputStream(filePath);43

44 String srcStr = "";45 if(in != null)46 {47 int byteNum =in.available();48 byte[] b = new byte[byteNum];49 in.read(b);//以字节流方式读入源文件

50 in.close();51 PrintBytes(b);52 //Convert

53 srcStr += new String(b, oldCharset); //生成的新的字符串是Unicode16(BE)编码的

54 byte[] nnb = srcStr.getBytes(newCharset);//获取Unicode16编码的字符串对应newCharset的字节数组,从而实现转码

55 PrintBytes(nnb);56 String destFilePath = destDir +fileName;57 OutputStream out = newFileOutputStream(destFilePath);58 out.write(nnb);//以字节流方式输出

59 out.flush();60 out.close();61 }62 }catch(Exception e)63 {64 e.printStackTrace();65 }66 }67

68 public static boolean readfile(String filepath, String destDir, String newCharset) throwsFileNotFoundException, IOException69 {70 try{71

72 File file = newFile(filepath);73 if (!file.isDirectory()) {74 System.out.println("文件");75 System.out.println("path=" +file.getPath());76 System.out.println("absolutepath=" +file.getAbsolutePath());77 System.out.println("name=" +file.getName());78 ConvertCharset(file.getAbsolutePath(), file.getName(), destDir, Convertor.Old_Charset, newCharset);79

80

81 } else if(file.isDirectory()) {82 System.out.println("文件夹");83 String[] filelist =file.list();84 for (int i = 0; i < filelist.length; i++) {85 File readfile = new File(filepath + "\\" +filelist[i]);86 if (!readfile.isDirectory()) {87 System.out.println("path=" +readfile.getPath());88 System.out.println("absolutepath="

89 +readfile.getAbsolutePath());90 System.out.println("name=" +readfile.getName());91 ConvertCharset(readfile.getAbsolutePath(), readfile.getName(), destDir, Convertor.Old_Charset, newCharset);92

93 } else if(readfile.isDirectory()) {94 readfile(filepath + "\\" +filelist[i], destDir, newCharset);95 }96 }97 }98

99 } catch(FileNotFoundException e) {100 System.out.println("readfile() Exception:" +e.getMessage());101 }102 return true;103 }104

105

106

107 public static void main(String args[]) throwsFileNotFoundException, IOException108 {109 //PrintAllSupportedCharset();110 //System.out.println(Charset.defaultCharset());

111 String srcPath = "H:\\LYDATA\\四国语言\\Unicode16\\Thai_Unicode\\";112 String destDir = "H:\\LYDATA\\四国语言\\Thai_TIS-620\\";113 readfile(srcPath, destDir, Convertor.TIS_620);114 }115 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值