从网上下载的项目,有时候.java文件的编码是ANSI。导入到自己的MyEclipse后,查看项目源码的时候,总是乱码。

一个个.java去修改的话, 既麻烦又不现实。所以写了下面这个工具类,进行批量转编码。

代码的原理仅仅就是遍历文件,然后使用流,对按照文件的原编码进行读取,用目的编码进行写操作。

直接上源码:

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; public class Main {
// private String sDirectory = "D:/Workspaces/BeyondDiscuz/src/com";
private String sDirectory = "D:\\Workspaces\\BeyondDiscuz\\src\\com";
private String dDirectory = "D:\\Workspaces\\BeyondDiscuz\\src\\cn"; public static void main(String[] args) {
Main 吗 = new Main();
try {
吗.readerFile(吗.sDirectory);
} catch (IOException e) {
e.printStackTrace();
}
} public void readerFile(String filePath) throws IOException {
if ("".equals(filePath) || null == filePath) {
return;
} File f = new File(filePath);
if (f.isDirectory()) {
String[] child = f.list();
for (int i = 0; i < child.length; i++) {
String path = f.getAbsolutePath() + File.separator;
String newPath = path.replace(this.sDirectory, this.dDirectory);
child[i] = path + child[i];
File c = new File(child[i]);
String newFile = child[i].replace(this.sDirectory, this.dDirectory);
System.out.println("旧路径:" + path);
System.out.println("新路径:" + newPath); File newP = new File(newPath);
if (!newP.exists())
newP.mkdir(); if (c.isFile()) {
System.out.println("旧文件:" + child[i]);
System.out.println("新文件:" + newFile);
// Charset US-ASCII ISO-8859-1 UTF-8 UTF-16BE UTF-16LE UTF-16
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(c), "GBK"));
// BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(c)));
File newF = new File(newFile);
newF.createNewFile();
// BufferedWriter w = new BufferedWriter(new
// OutputStreamWriter(new FileOutputStream(newF), "UTF-8"));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newF)));
// BufferedWriter w = new BufferedWriter(new FileWriter(newFile));
String lineText = null;
while ((lineText = r.readLine()) != null) {
// String temp = new String(lineText.getBytes("ISO-8859-1"), "UTF-8");
w.write(lineText);
w.newLine();
}
w.close();
r.close();
} else {
readerFile(child[i]);
}
}
}
} }

上面自己的写代码只是基本思路,下面是更简洁的代码:

public static void main(String[] args) {
try {
String srcDirPath = "E:\\eclipseWorkspaceSandbase\\test";
// 转为UTF-8编码格式源码路径
String utf8DirPath = "E:\\eclipseWorkspaceSandbase\\test-8"; // 获取所有java文件
Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[] { "java" }, true); for (File javaGbkFile : javaGbkFileCol) {
System.out.println(javaGbkFile);
// UTF8格式文件路径
String utf8FilePath = utf8DirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
// 使用GBK读取数据,然后用UTF-8写入数据
FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
}
} catch (Exception e) {
e.printStackTrace();
} }

[原创]使用java批量修改文件编码(ANSI-->UTF-8)的更多相关文章

  1. Java 批量修改文件夹里面的文件的名字

    背景:公司要求使用PADS完成原理图设计.PCB Layout.而他硬件工程师要求我在将PADS的库文件发送给他们之前,必须在每一个库文件的后面追加今天的日期,再发送给他们. 问题来了,如果一次需要发 ...

  2. 分享一个批量修改文件编码的python脚本

    分享一个自己编写的递归查找子目录,将所有cpp文件编码修改为utf-8编码格式的小脚本 #i!/usr/bin/env python3 # -*- coding:utf-8 -*- import os ...

  3. Linux下批量修改文件编码

    假设需要将所有afish目录下的php文件,编码从gb2312转到utf8 cd afish find ./ -type f -name “*.php”|while read line;do echo ...

  4. Java 批量修改文件后缀

    import java.io.*; public class test { public void reName(String path, String from, String to) { File ...

  5. iconv 批量修改文件编码

    iconv_shell.sh #!/bin/bash "];then echo "Usage: `basename $0` dir filter" exit fi dir ...

  6. Java实现批量修改文件名称

    import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; /** ...

  7. Windows批量修改文件

    如图我是建立了壁纸文件夹 Windows自带的排序方式 如何不用自带的呢? 在这个文件夹里面建一个.txt文件 如下 ok第二步骤 将UTF-8格式改为ANSI格式 点击文件-另存为ANSI格式-替换 ...

  8. Ubuntu 查看/修改文件编码

    使用enca工具可以查看和修改文件编码 1.安装 sudo apt-get install enca 2.使用 查看文件编码 enca –L zh_CN file_name 修改文件编码 enca – ...

  9. 用SSH指令批量修改文件夹 文件权限和拥有者

    在linux系统下或登录ssh可以批量修改文件权限 wwwroot目录下的所有目录的权限递归设置为755 cd wwwrootfind -type d -exec chmod 755 {} \;或者是 ...

随机推荐

  1. %1 不是有效的Win32应用程序

    用 64bit 的 Python 调用 32bit 的 Dll 会出错

  2. Photoshop 融合属性 Unity Shader

    http://forum.unity3d.com/threads/free-photoshop-blends.121661/

  3. js正则,电话,邮箱

    1. <script type="text/javascript"> var str="Is this all th05777-89856825ere is5 ...

  4. TEXshade教程- 多重比对着色软件包

    多重比对着色软件包 TEXshade 图解安装教程   [絮语]: TEXshade 是 Latex 的一个宏包,可以对 MSF或 ALN 格式的多重比对文件以不同的方式进行着色美化,并可以对重要的位 ...

  5. EasyDarwin

     1.EasyDarwin多媒体点播实现 新版本EasyDarwin都加入了中心管理服务EasyCMS, 对于点播系统,EasyCMS不是必须的. MP4点播,需要将mp4文件hint之后才行,MP4 ...

  6. union联合体使用详解

    1.联合体联合体(union)与结构体(struct)有一些相似之处.但两者有本质上的不同.在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和.而在联合体中,各成员共享一段内存 ...

  7. Cucumber命令行接口

    1. cucumber的命令行选项 首先查看命令行选项.和其它命令行工具一样,cucumber提供了—help选项.下面是cucumber帮助的一个缩减版本: $ cucumber --help -r ...

  8. linux shell中判断bash脚本输入的参数个数

    看下面的一段程序. #!/bin/bash ]; then echo "参数个数为$#个" else echo "没有参数" fi

  9. CF448C Painting Fence (分治递归)

    Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...

  10. webpack入门(一)——webpack 介绍

    如今的网站正在演化为web应用程序: 1. 越来越多的使用JavaScript. 2. 现代浏览器提供更广泛的接口. 3. 整页刷新的情况越来越少,甚至更多代码在同一个页面.(SPA) 因此有很多代码 ...