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文件

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

用java修改文件的编码的更多相关文章

  1. Java修改文件夹名称

    Java修改文件夹名称 学习了:http://blog.csdn.net/yongh701/article/details/45063833 进行文件夹名字批量修改,注意,要写全路径: package ...

  2. 批量将Java源代码文件的编码从GBK转为UTF-8

    主要参考: http://blog.csdn.net/liu_qiqi/article/details/38706497 使用common io批量将java编码从GBK转UTF-8 http://w ...

  3. eclipse实现批量修改文件的编码方式

    http://blog.csdn.net/haorengoodman/article/details/38493007 在eclipse+MyEclipse环境下,打开一个jsp文件,经常发现汉字无法 ...

  4. 如何利用eclipse实现批量修改文件的编码方式

        在eclipse+Eclipse环境下,打开一个jsp文件,经常发现汉字无法显示,右键点击查看这个文件属性,发现文件的字符编码属性为ISO-8859-1.    目前的解决方法有:1. 手工把 ...

  5. java修改文件内容

    文件的读和写,大家都不陌生,但是修改呢?按照普通的读写流去修改的话,只能全部读取出来,在内存中修改好后,全部写进去,这样对于文件内容过多的时,性能很低. 最近在遇到这个问题的时候,发现RandomAc ...

  6. Eclipse:批量将Java源代码文件的编码从GBK转为UTF-8

    很简单的几行代码,就可以批量将GBK格式的java文件转为UTF-8格式. 基本上所有文本文件的编码转换都可以采用这种方式. import java.io.File; import java.io.I ...

  7. java修改文件所有者及其权限

    1.设置所有者 管理文件所有者 Files.getOwner()和Files.setOwner()方法 要使用UserPrincipal来管理文件的所有者 (1)更改文件的所有者 import jav ...

  8. 如何将Java源代码文件的编码从GBK转为UTF-8?

    有时候看到有意思的demo,在头痛导入项目的编码和workspace的编码不一样的时候 我试着将 笔记本打开一个类一个类的复制, demo的类比较少的时候 可以忍受,demo的类多的时候 除了靠之外 ...

  9. JMeter中用java修改文件名称

    import java.io.File; String NewDataPath=bsh.args[0]; File SrcFile= new File(NewDataPath+"AutoTe ...

随机推荐

  1. Activity生命周期,切换,参数传递,bundle(包),值对象,Activity参数返回,Activity的启动模式

    Activity代表手机屏幕的一屏,或是平板电脑中的一个窗口.它是android应用中最重要的组成单元之一,提供了和用户交互的可视化界面.在一个Activity中,可以添加很多组件,这些组件负责具体的 ...

  2. javascript:FileReader对象(读取文件)

    FileReader对象 1.检测浏览器对FileReader的支持 1 if(window.FileReader) { 2 var fr = new FileReader(); 3 // add y ...

  3. Firebird日期时间操作

    最近在使用Firebird数据做 一项目,使用FireBird边用边学.(以下转贴) 查询2007年度以后的,12月份以上的数据记录,datetime为timestamp字段 select * fro ...

  4. 使用exec函数将当前的信息输入到文件中

    先来看看exec函数: exec函数族     fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个程序.当进程调用一种exec函 ...

  5. 解决error: only position independent executables (PIE) are supported

    在Android.mk文件中添加以下内容 LOCAL_CFLAGS += -pie -fPIE LOCAL_LDFLAGS += -pie -fPIE 原帖地址:http://blog.csdn.ne ...

  6. delphi 实现用户自定义通知(User Notification)

    unit Form_Main; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Sy ...

  7. 24.类的加载机制和反射.md

    目录 1类的加载连接和初始化 1.1类的加载过程 1.2类的加载器 1.2.1类的加载机制 1类的加载连接和初始化 1.1类的加载过程 类的加载过程简单为分为三步:加载->连接->初始化 ...

  8. Ldap-crack-test?

    ldap #!/bin/env python import sys import ldap ldapconn = ldap.initialize('ldap://domain.adserve.com' ...

  9. Structs复习 Action

    引入jar包 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  10. python垃圾回收机制(转)

    Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...