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. 把网卡中断绑定到CPU,最大化网卡的吞吐量(转)

    先来看一下问题, 我们通过 ifconfig 查看接口的名称 为 p15p1, 一般机器为 eth0 再通过命令 ➜ ~ cat /proc/interrupts | head -n 1 && ...

  2. 尚硅谷springboot学习13-配置的加载

    配置可以有很多不同的来源,也有不同的加载顺序,下面来具体的看一下 配置文件加载位置 spring boot 启动会扫描以下位置的application.properties或者application. ...

  3. Linux通配符与基础正则表达式、扩展正则表达式

    在Linux命令行操作或者SHELL编程中总是容易混淆一些特殊字符的使用,比如元字符‘*’号,作为通配符匹配文件名时表示0个到无穷多个任意字符.而作为正则表达式匹配字符串时,表示重复0个到无穷多个的前 ...

  4. LeetCode OJ 77. Combinations

    题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...

  5. Ubuntu下的LNMP环境

    保证联网的情况下,直接参照http://lnmp.org/install.html进行安装,以下花括号内为原文引用: { 1.使用putty或类似的SSH工具登陆VPS或服务器: 登陆后运行:scre ...

  6. DNS协议工作过程;DNS的安全隐患

    DNS协议工作过程   下面以域名为m.xyz.com的主机欲通过另一个主机的域名y.abc.com的IP地址为例,简述DNS协议过程. 主机m.xyz.com先向其本地服务器dns.xyz.com进 ...

  7. 利用ant脚本 自动构建svn增量/全量 系统程序升级包【转】

    引文:我们公司是做自己使用产品,迭代更新周期短,每次都花费较多时间和精力打包做增量更新,发现了一篇文章用于 自动构建svn增量/全量 系统程序升级包,收藏之,希望可以通过学习,更加简化我们的工作. 文 ...

  8. 吴裕雄 16-MySQL UNION 操作符

    描述MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多个 SELECT 语句会删除重复的数据. 语法MySQL UNION 操作符语法格式:SELECT ...

  9. elasticsearch 测试

    https://www.yiibai.com/elasticsearch/elasticsearch-getting-start.html # curl -XPUT "http://loca ...

  10. 2-glance 部署

    1. mysql 创建数据库和用户 create database glance; grant all privileges on glance.* to 'glance'@'localhost' i ...