中文乱码真的是让人很头疼问题,有了这个方法应该能缓解这种头疼,用的是递归方式查找文件,直接在原文件中修改,小心使用(在本地测试效果有点诡异呀,没有达到预期效果)。

 package com.hy.util;

 import info.monitorenter.cpdetector.io.*;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; public class CharacterChange{ public static void main(String[] args) throws FileNotFoundException, IOException { // 封装目录,需要修改文件格式的路径
File srcFolder = new File("F:\\test"); String newCharater = "GBK"; getAllJavaFilePaths(srcFolder, newCharater);
} private static void getAllJavaFilePaths(File srcFolder, String newCharater) throws IOException { // 获取该目录下所有的文件或者文件夹的File数组
File[] fileArray = srcFolder.listFiles(); // 遍历该File数组,得到每一个File对象
for (File file : fileArray) { // 继续判断是否以特定文件结尾,不是的话继续调用getAllJavaFilePaths()方法
if (file.isDirectory()) {
getAllJavaFilePaths(file, newCharater);
} else {
if (file.getName().endsWith(".sql")) {
try {
FileInputStream fis = new FileInputStream(file);
//oldcCharacter 获取特定的字符集
String oldcCharacter = getChartsetName(file);
InputStreamReader isr = new InputStreamReader(fis, oldcCharacter);
BufferedReader br = new BufferedReader(isr);
String str = null;
// 创建StringBuffer字符串缓存区
StringBuffer sb = new StringBuffer();
// 通过readLine()方法遍历读取文件
while ((str = br.readLine()) != null) {
// 使用readLine()方法无法进行换行,需要手动在原本输出的字符串后面加"\n"或"\r"
str += "\n";
sb.append(str);
}
String fileSource = sb.toString();
// 以GBK格式写入文件,file.getAbsolutePath()即该文件的绝对路径,false代表不追加直接覆盖,true代表追加文件
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath(), false);
OutputStreamWriter osw = new OutputStreamWriter(fos, newCharater);
try {
osw.write(fileSource);
System.out.println(
"将:" + oldcCharacter + " 的文件:" + file.getAbsolutePath() + "修改字符集为:" + newCharater);
} finally {
osw.flush();
osw.close();
fos.close();
br.close();
isr.close();
fis.close();
}
} catch (Exception e) {
}
} else {
System.err.println("该文件以忽略:" + file.getAbsolutePath());
}
}
}
} public static String getChartsetName(File file) {
String chartsetName = null;
// 获取文件编码格式
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
detector.add(new ParsingDetector(true));
detector.add(JChardetFacade.getInstance());
detector.add(ASCIIDetector.getInstance());
detector.add(UnicodeDetector.getInstance());
java.nio.charset.Charset charset = null;
try {
if (file != null) {
charset = detector.detectCodepage(file.toURL());
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (charset != null) {
chartsetName = charset.name();
} else {
chartsetName = "未知的编码";
}
return chartsetName;
}
/*
* <dependency>
<groupId>net.sourceforge.jchardet</groupId>
<artifactId>jchardet</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
*/
}

用java转换文件的字符集的更多相关文章

  1. java实现文件转换成二进制存储与取出

    一.功能描述: 将文件转成二进制数据放入数据库中,需要的时候,便可以取出安装与使用. 二.数据库: 建立一个数据库字段存放转成二进制的图片,这个字段有一个要求就是要设置成blob类型的 CREATE  ...

  2. java实现 批量转换文件编码格式

    一.场景说明 不知道大家有没有遇到过之前项目是GBK,现在需要全部换成UTF-8的情况.反正我是遇到了. eclipse可以改变项目的编码格式,但是文件如果直接转换的话里面的中文就会全部乱码,需要先复 ...

  3. java中调用kettle转换文件

    java中调用kettle转换文件 通过命令行也能够调用,然后java中调用命令行代码也能够.这样没有和java代码逻辑无缝集成.本文说明kettle5.1中假设通过其它API和java代码无缝集成: ...

  4. java读取文件批量插入记录

    只是一个例子,方便以后查阅. import ey.db.oracle.OracleHelper; import ey.db.type.*; import java.io.BufferedReader; ...

  5. Java IO 文件与流基础

    Java IO 文件与流基础 @author ixenos 摘要:创建文件.文件过滤.流分类.流结构.常见流.文件流.字节数组流(缓冲区) 如何创建一个文件 #当我们调用File类的构造器时,仅仅是在 ...

  6. oracle 如何查询/修改dmp文件的字符集

    1.如何查询dmp文件的字符集 用oracle的exp工具导出的dmp文件也包含了字符集信息,dmp文件的第2和第3个字节记录了dmp文件的字符集.如果dmp文件不大,比如只有几M或几十M,可以用Ul ...

  7. java 读写文件例子

    在linux下可以读写中文 import java.io.*; import java.text.SimpleDateFormat; import java.util.*; public class ...

  8. java向文件中添加数据---手动版日志添加

    核心代码为创建多级文件夹创建 //目标文件 File file=new File(filePath); //若不存在即创建文件 if(!file.exists()) { if (!file.getPa ...

  9. java写文件实现换行

    Java 写文件实现换行   第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exist ...

随机推荐

  1. 012_使用死循环实时显示 eth0 网卡发送的数据包流量

    #!/bin/bash while : do echo '本地网卡 eth0 流量信息如下: ' #grep输出所找整行,awk直接输出第5列 ifconfig eth0 | grep "R ...

  2. [Luogu] 均分数据

    题面:https://www.luogu.org/problemnew/show/P2503 题解:https://www.zybuluo.com/wsndy-xx/note/1141736

  3. 1~100卡特兰数(存一下hhhh)

    1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 35357670 129644790 477638700 ...

  4. c++容器 算法 迭代

    #include <iostream> #include <vector> using namespace std; int main() { // 创建一个向量存储 int ...

  5. leveldb源码分析之Slice

    转自:http://luodw.cc/2015/10/15/leveldb-02/ leveldb和redis这样的优秀开源框架都没有使用C++自带的字符串string,redis自己写了个sds,l ...

  6. jenkins之自动化部署github上maven项目

    部署流程:将代码从github上拉取下来,使用maven打包,将打包后的jar通过ssh发送到服务器上,然后构建docker镜像,运行容器. 1.安装插件 如果是第一次使用jenkins,需要检查并确 ...

  7. jinja2-宏,include, import

    一 宏 宏类似常规编程语言中的函数.它们用于把常用行为作为可重用的函数,取代 手动重复的工作.如果宏在不同的模板中定义,你需要首先使用 import,比如 {% macro input(name, v ...

  8. mysql 远程登陆

    1.查询mysql是否启动 netstat  -lnp|grep   3306 ps -df |grep  mysqld 2.通过TCPIP的方式测试连接 mysql -uqingjiao -padm ...

  9. java -cp 用法介绍

    java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库,jar包之类,需要全路径到jar包,window上分号“;” 分隔,linux上是分号“:”分隔.不支持通 ...

  10. linux内核中的wait_event_interruptible_timeout接口解析

    1. 原型 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \ ({ \ long __ret = time ...