您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 利用Java代码实现无字符+中文转换为全中文的代码
  所属分类: Java
  开发工具:
  文件大小: 4kb
  下载次数: 0
  上传时间: 2019-05-05
  提 供 者: qq_42******
 详细说明: 大多源码来自互联网,本人只做部分正和 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import com.vince.*; /** * 将本地文件以哪种编码输出 * param inputfile 输入文件的路径 * param outfile 输出文件的路径 * param code 输出文件的编码 * throws IOException */ public class Charchange{ public static void main(String[] args) throws IOException { String inputfile,outputfile,code; inputfile = "D:\\迅雷\\work\\fen\\2-temp-test.txt";//要转码的文件 outputfile = "D:\\迅雷\\work\\1.txt";//输出的文件 code = "utf-8"; System.out.println("转码开始"); convert(inputfile,outputfile,code); System.out.println("转码完成"); } public static void convert(String inputfile,String outfile,String code) throws IOException { StringBuffer sb = new StringBuffer(); StringBuffer sb2 = new StringBuffer(); //得到当前文件的编码 String ch=getCharset(inputfile); InputStreamReader isr=null; OutputStreamWriter osw =null; //根据当前文件编码进行解码 if(ch.equals("UTF8")){ isr= new InputStreamReader(new FileInputStream(inputfile), "UTF-8"); }else if(ch.equals("Unicode")){ isr= new InputStreamReader(new FileInputStream(inputfile), "Unicode"); }else { isr= new InputStreamReader(new FileInputStream(inputfile), "GB2312"); } //将字符串存入StringBuffer中 BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); isr.close(); //以哪种方式写入文件 if("UTF-8".equals(code)){ osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"); }else if("GB2312".equals(code)){ osw = new OutputStreamWriter(new FileOutputStream(outfile), "GB2312"); }else if("Unicode".equals(code)){ osw = new OutputStreamWriter(new FileOutputStream(outfile), "Unicode"); }else{ osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"); } BufferedWriter bw = new BufferedWriter(osw); String sb1 = sb.toString(); String a1 = deal(sb1); bw.write(a1); bw.close(); osw.close(); } /** * 根据文件路径判断编码 * param str * return * throws IOException */ private static String getCharset(String str) throws IOException{ BytesEncodingDetect s = new BytesEncodingDetect(); String code = BytesEncodingDetect.javaname[s.detectEncoding(new File(str))]; return code; } //本方法完成单个无字符的转换 public static String Change(String temp){ String myString = temp.replace("&#", ""); String[] split = myString.split(";"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < split.length; i++) { sb.append((char)Integer.parseInt(split[i])); } return sb.toString(); } //接收String sb1并对字符串的联合处理 public static String deal(String sb1) { //模块化开始 String car="";//小车运输单个字符 while(sb1.length()!=0){ int markStar = sb1.indexOf("&"); //判断方法是以&开头的数据默认为要处理的无字符 if(markStar==0){ String temp = sb1.substring(markStar,8); car = car+Change(temp); sb1=sb1.substring(8); }else if(markStar==-1&sb1;.length()>0){ String temp = sb1.substring(0,sb1.length()); car = car+temp; sb1=sb1.substring(sb1.length()); }else{ String temp = sb1.substring(0,markStar); car = car+temp; sb1=sb1.substring(markStar); } } return car.toString() ; } }
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索:
 输入关键字,在本站1000多万海量源码库中尽情搜索: