针对以&#开头,以;结尾的文档转中文的一些想法

针对以&#开头,以;结尾的文档转中文的一些想法

起因是发现一些txt文档中混有夯所以小说看不懂,于是二话不说盘它,先是在网上搜知道他的真实身份 ,接着在看了网上的大身的代码和测试后有一些想法,为了省事借用网上大佬的代码,代码如下

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;
/**
 * 将本地文件以哪种编码输出
 * @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() ;
}
}

我感觉deal方法应该可以再优化一些希望大老再给些思路
我的方法是对String字符串进行单个判断并重写
最后借助流输出到文件

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值