[原创]使用java批量修改文件编码(ANSI-->UTF-8)
从网上下载的项目,有时候.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)的更多相关文章
- Java 批量修改文件夹里面的文件的名字
背景:公司要求使用PADS完成原理图设计.PCB Layout.而他硬件工程师要求我在将PADS的库文件发送给他们之前,必须在每一个库文件的后面追加今天的日期,再发送给他们. 问题来了,如果一次需要发 ...
- 分享一个批量修改文件编码的python脚本
分享一个自己编写的递归查找子目录,将所有cpp文件编码修改为utf-8编码格式的小脚本 #i!/usr/bin/env python3 # -*- coding:utf-8 -*- import os ...
- Linux下批量修改文件编码
假设需要将所有afish目录下的php文件,编码从gb2312转到utf8 cd afish find ./ -type f -name “*.php”|while read line;do echo ...
- Java 批量修改文件后缀
import java.io.*; public class test { public void reName(String path, String from, String to) { File ...
- iconv 批量修改文件编码
iconv_shell.sh #!/bin/bash "];then echo "Usage: `basename $0` dir filter" exit fi dir ...
- Java实现批量修改文件名称
import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; /** ...
- Windows批量修改文件
如图我是建立了壁纸文件夹 Windows自带的排序方式 如何不用自带的呢? 在这个文件夹里面建一个.txt文件 如下 ok第二步骤 将UTF-8格式改为ANSI格式 点击文件-另存为ANSI格式-替换 ...
- Ubuntu 查看/修改文件编码
使用enca工具可以查看和修改文件编码 1.安装 sudo apt-get install enca 2.使用 查看文件编码 enca –L zh_CN file_name 修改文件编码 enca – ...
- 用SSH指令批量修改文件夹 文件权限和拥有者
在linux系统下或登录ssh可以批量修改文件权限 wwwroot目录下的所有目录的权限递归设置为755 cd wwwrootfind -type d -exec chmod 755 {} \;或者是 ...
随机推荐
- FBX
http://docs.autodesk.com/FBX/2014/ENU/FBX-SDK-Documentation/index.html http://forums.autodesk.com/t5 ...
- Webpack 之 Loader 的使用
安装 loaders 如果loader在npm里,可以这样安装: $ npm install xxx-loader --save 或者 $ npm install xxx-loader --save- ...
- 安装glue,用glue批量处理图片的步骤
glue批量处理图片:http://glue.readthedocs.io/en/latest/quickstart.html#and-why-those-css-class-names 首先需要安 ...
- js中的preventDefault与stopPropagation详解
本篇文章主要是对js中的preventDefault与stopPropagation进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 首先讲解一下js中preventDefault和stopP ...
- HTML5基本布局
HTML4布局 HTML5布局 基本的HTML5文档的模式为: <!DOCTYPE html> <html lang = "en"> <head> ...
- iOS直播点赞动画,iOS直播心型点赞动画
https://github.com/songxing10000/LikeAnimation-PraiseAnimation
- Servlet的配置
让 Servlet 能响应用户请求,必须将 Servlet 配置在 Web 应用中. Servlet 3.0 中有两中配置方式: 1. 在 Servlet 类中使用 @WebServle ...
- springmvc的form标签
1.要使用Spring MVC提供的表单标签,首先需要在视图页面添加: <%@ taglib prefix="form" uri="http://www.sprin ...
- .NET Core、DNX、DNU、DNVM、MVC6学习资料
一.资源 1.http://dotnet.github.io/ 2.http://www.codeproject.com/Articles/1005145/DNVM-DNX-and-DNU-Under ...
- Linux下更新时间
方法一.使用命令 ntpdate time-a.nist.gov 方法二.本地安装ntpdate客户端 在本地安装ntpdate客户端,更新时用 ntpdate cn.pool.ntp.org 如果你 ...