用java修改文件的编码

时间:2024-10-14 08:33:38

1、将本地的文件转换成另外一种编码输出,主要逻辑代码如下:

  /**
* 将本地文件以哪种编码输出
* @param inputfile 输入文件的路径
* @param outfile 输出文件的路径
* @param code 输出文件的编码
* @throws IOException
*/
public void convert(String inputfile,String outfile,String code) throws IOException {
StringBuffer sb = 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);
bw.write(sb.toString());
bw.close();
osw.close();
} /**
* 根据文件路径判断编码
* @param str
* @return
* @throws IOException
*/
private String getCharset(String str) throws IOException{
BytesEncodingDetect s = new BytesEncodingDetect();
String code = BytesEncodingDetect.javaname[s.detectEncoding(new File(str))];
return code;
}

2、将远程的文件转换成自己想要的编码,然后写入本地

 /**
* 将远程文件以哪种编码输出到本地
* @param fileurl 远程文件的URL
* @param outfile 输出文件的路径
* @param code 以哪种编码输出
* @throws Exception
*/
public void Remote(String fileurl,String outfile,String code) throws Exception{
// String str="http://172.18.1.103:8080/kms/text.txt";
URL url =new URL(fileurl);
HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
//设置连接超时和读取超时
urlCon.setConnectTimeout(5000);
urlCon.setReadTimeout(5000);
//得到远程文件的编码
String ch=getCharset_Remote(fileurl);
//文件写入流
OutputStreamWriter osw =null;
BufferedReader br=null;
//将字符串保存到临时的StringBuffer中
StringBuffer sb = new StringBuffer();
if(ch.equals("UTF8")){
br =new BufferedReader(new InputStreamReader( urlCon.getInputStream(),"UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
}
else if(ch.equals("Unicode")){
br =new BufferedReader(new InputStreamReader( urlCon.getInputStream(),"Unicode"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
}
else{
br =new BufferedReader(new InputStreamReader( urlCon.getInputStream(),"GB2312"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
}
br.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);
bw.write(sb.toString());
bw.close();
osw.close();
} /**
* 根据文件路径判断远程文件编码
* @param str
* @return
* @throws IOException
*/
private String getCharset_Remote(String str) throws IOException{
URL url =new URL(str);
BytesEncodingDetect s = new BytesEncodingDetect();
String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(url)];
return fileCode;
}

3、测试代码:

 package com.zhang.test;

 public class test {
public static void main(String[] args) {
String inputfile="D:/gbk.txt";
String outfile="D:/utf8.txt";
String outfileurl="D:/utf8url.txt";
String fileurl="http://127.0.0.1:8080/jsp/gbk.txt";
// String file="D:/unicode.txt";
Convert_Code code=new Convert_Code();
try {
code.convert(inputfile,outfile,"UTF-8");
code.Remote(fileurl, outfileurl, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}
}

注意:这两个方法引用了BytesEncodingDetect.java文件

用java修改文件的编码

用java修改文件的编码

此案例的下载链接:http://download.****.net/detail/u013865056/9908100

相关文章