[原创]使用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 {} \;或者是 ...
随机推荐
- url下载网页的三种方法
# -*- coding: utf-8 -*- import cookielib import urllib2 url = "http://www.baidu.com" print ...
- 自然语言17_Chinking with NLTK
https://www.pythonprogramming.net/chinking-nltk-tutorial/?completed=/chunking-nltk-tutorial/ 代码 # -* ...
- 11 Clever Methods of Overfitting and how to avoid them
11 Clever Methods of Overfitting and how to avoid them Overfitting is the bane of Data Science in th ...
- 一张图总结docker命令
- XtraFinder到底好在哪里(标签、隐藏文件、路径拷贝与显示、从这里启动)
类似Chrome的标签 自定义工具栏可添加一个快速显示与隐藏当前目录隐藏文件的功能 拷贝路径 cd 到当前目录这个功能跟PathFinder7类似 当然还有其他很多功能,比如这个返回上级目录,
- .NET软件开发与常用工具清单(转)
http://www.cnblogs.com/smileberry/p/4047835.html
- Servlet的配置
让 Servlet 能响应用户请求,必须将 Servlet 配置在 Web 应用中. Servlet 3.0 中有两中配置方式: 1. 在 Servlet 类中使用 @WebServle ...
- 1.servlet的会话机制cookie
会话:用户开浏览器访问某个网站,只要不关闭浏览器,不管该用户点击多少个超链接,访问多少资源,直到用户关闭浏览器,整个过程称为一次会话 cookie会话: 1.记录用户上次登录的时间 2.浏览商品的历史 ...
- HTTP 学习
*** *** http://www.w3school.com.cn/xml/xml_http.asp *** *** http://www.cnblogs.com/shenliang123/arch ...
- ASP.NET MVC5 Filter重定向问题
ASP.NET MVC5 Filter重定向问题 一.问题描述 1.在Filter中使用直接filterContext.RequestContext.HttpContext.Response.Redi ...