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. compose配置文件参数详解

    转自:https://www.cnblogs.com/jsonhc/p/7814138.html 本文介绍compose配置文件参数的使用,熟练编写compose文件 [root@docker lnm ...

  2. 趣味编程:静夜思(Python版)

    from itertools import groupby def verticalWriting(txt, offset): l = lambda x: x[0] % offset for (_, ...

  3. shell流程控制与循环结构

    shell脚本中的流程控制有if/else语句.case语句,循环结果包括for循环.while循环.until循环等内容. if语句 (1)最简单的if语句.使用格式有2种方式,分别如下 使用格式1 ...

  4. LeetCode OJ 49. Group Anagrams

    题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...

  5. Android 设备,如何root,执行adb shell,查看设备中的数据库信息等【转】

    原文地址: Android 设备,如何root,执行adb shell,查看设备中的数据库信息等

  6. 在Centos 6.5 X64下切割m3u8

    操作系统:centos 6.5 必需要参考的文章: http://blog.chinaunix.net/uid-23069658-id-4018842.html 准备工作: 安装git yum ins ...

  7. Kubernetes1.9 二进制版集群+ipvs+coredns

    节点构造如下 : 节点ip 节点角色 hostname 192.168.0.57 node bigdata3       192.168.0.56 node bigdata4       192.16 ...

  8. Kotlin系列之序列(Sequences)源码完全解析

    Kotlin系列之序列(Sequences)源码完全解析 2018年06月05日 22:04:50 mikyou 阅读数:179 标签: Kotlin序列(sequence)源码解析Androidja ...

  9. redis异常Redis:java.util.NoSuchElementException: Unable to validate object at

    前两天项目上线的时候遇到了redis的一个问题,在测试环境的时候项目运行正常,项目一上线redis便开始抛异常. redis.clients.jedis.exceptions.JedisConnect ...

  10. 导出pdf

    document.getElementById("exportSiteInfoTemp").onclick = function() { var thisMinheight=$(& ...