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. Mysql(MyISAM和InnoDB)及Btree和索引优化

    MYSQL 一.引擎 mysql:MySQL是一个关系型数据库管理系统,其中有两种引擎最为常见MyISAM和InnoDB MyISAM(非聚集索引)  MySQL 5.0 之前的默认数据库引擎,最为常 ...

  2. 跨域(二)——WebSocket

    严格地说,WebSocket技术不属于HTML5,这个技术是对HTTP无状态连接的一种革新,本质就是一种持久性socket连接,在浏览器客户端通过javascript进行初始化连接后,就可以监听相关的 ...

  3. csredis base usage

    Basic usage Whenever possible, server responses are mapped to the appropriate CLR type. using (var r ...

  4. 手工获取AWR报告

    AWR(Automatic Workload Repository)报告常用于Oracle数据库性能分析.熟练解读AWR报告有助于快速分析Oracle性能问题.下面主要描述如何手工获取AWR报告. 操 ...

  5. node-sass:npm install node-sass --save

    从git上拉下来的项目,要先安装依赖, 再运行. 缺少node-sass:npm install node-sass --save

  6. mybatis实现一对多连接查询

    问题:两个对象User和Score,它们之间的关系为一对多. 底层数据库为postgresql,ORM框架为mybatis. 关键代码如下: mybatis配置文件如下: mybatis.xml文件内 ...

  7. 关于在VS2008和VS2010中禁用及卸载Visual Assist X的方法研究——转载

    禁用和启用   此方法对于VS2008和VS2010 都适用. 在VS2008或VS2010菜单栏中选择“VassistX”选项卡,找到“Enable/Disable Visual Assist X” ...

  8. ROS:ROS操作类MK.cs

    class MK { Stream connection; TcpClient con; public MK(string ip,int port) { con = new TcpClient(); ...

  9. Win2012&Win2008双系统启动菜单设置

    电脑最初安装的是XP,后来想升级操作系统,但XP里又有很多常用软件不想重装,于是装了一个Win2008 R2的双系统,安装好2008R2后,系统自动产生一个2008R2的启动菜单,可以选择进入2008 ...

  10. Pandas分类

    Pandas分类 categorical data是指分类数据:数据类型为:男女.班级(一班.二班).省份(河北.江苏等),若使用赋值法给变量赋值,例如(男=1,女=0),数字1,0之间没有大小之分, ...