[原创]使用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 {} \;或者是 ...
随机推荐
- Java数据结构——栈
//================================================= // File Name : Stack_demo //-------------------- ...
- Timer类和TimerTask类
Timer类是一种线程设施,可以用来实现在某一个时间或某一段时间后安排某一个任务执行一次或定期重复执行. 该功能要与TimerTask类配合使用.TimerTask类用来实现由Timer安排的一次或重 ...
- cmake 静态调用 c++ dll 的类的一个例子(Clion IDE)[更新1:增加1.模版的应用,2.ma 的算法]
CMakeLists.txt project(aaa) add_library(aaa SHARED aaa.cpp) add_executable(bbb bbb.cpp) target_link_ ...
- C#如何把List of Object转换成List of T具体类型
上周码程序的时候碰到个问题,因为设计上的约束,一个方法接受的参数只能为List<object>类型,然而该方法需要处理的真实数据则是确定的List<Currency>.然而C# ...
- centos 7.0 菜鸟接触命令 记录
centos 7.0 最小化安装 查看IP ip addr 查看外网IP curl ifconfig.me 重启 shutdown -r now 安装wget yum -y install wget ...
- State Threads——异步回调的线性实现
State Threads——异步回调的线性实现 原文链接:http://coolshell.cn/articles/12012.html 本文的标题看起来有点拗口,其实State Threads库就 ...
- HTTP1.0与HTTP1.1的区别
HTTP/1.1与HTTP/1.0的区别 下面主要从几个不同的方面介绍HTTP/1.0与HTTP/1.1之间的差别,当然,更多的内容是放在解释这种差异背后的机制上. 1 可扩展性 可扩展性的一个重要原 ...
- 防SQL注入代码(ASP版)
<% Dim Fy_Url,Fy_a,Fy_x,Fy_Cs(),Fy_Cl,Fy_Ts,Fy_Zx '---定义部份 头------ Fy_Cl = 1 '处理方式:1=提示信息,2=转向页面, ...
- [问题] UISearchBar 点击取消后跳动的问题
问题详情: 首先是TableView 作为 NavigationController 的 RootViewContrller, 然后UISearchBar 添加到TableView 的 headV ...
- MVC下分页的自定义分页一种实现
1.引言 在MVC开发中我们经常会对数据进行分页的展示.通过分页我们可以从服务端获取指定的数据来进行展示.这样既节约了数据库查询的时间也节约了网络传输的数据量.在MVC开发中使用的比较多的应该是MVC ...