本文主要是用Java实现文件编码格式的转换,处理各种乱码问题。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Vector;
public class AnsiToUtf8
{
public static void main(String[] args) throws UnsupportedEncodingException,
IOException
{
ReadFileName tmp = new ReadFileName();
Vector<String> vec = tmp.FileNameOftype("file\\comments", ".txt");
int size = vec.size();
System.out.println(size);
for (int i = 0; i < size; i++)
{
change(vec.get(i));
System.out.println(vec.get(i) + " " + i);
}
}
public static void change(String filepath)
throws UnsupportedEncodingException, IOException
{
BufferedReader buf = null;
OutputStreamWriter pw = null;
String str = null;
String allstr = "";
// 用于输入换行符的字节码
byte[] c = new byte[2];
c[0] = 0x0d;
c[1] = 0x0a;
String t = new String(c);
buf = new BufferedReader(new InputStreamReader(new FileInputStream(
filepath), "gb2312")); //gb2312转成其他的编码
/* GBK转成其体的编码
buf = new BufferedReader(new InputStreamReader(new FileInputStream(
filepath), "GBK"));*/
while ((str = buf.readLine()) != null)
{
allstr = allstr + str + t;
}
buf.close();
pw = new OutputStreamWriter(new FileOutputStream(filepath), "UTF-8"); //最终要转换成的文件的编码格式是utf-8
pw.write(allstr);
pw.close();
}
}
class ReadFileName
{
// 存储遍历获取的所有文件名
private Vector<String> filelist = new Vector<String>();
// 遍历指定路径下的所有文件
// 以ArrayList存储于filelist中
private void RefreshFileList(String strpath)
{
File dir = new File(strpath);
File[] files = dir.listFiles();
if (files == null)
return;
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
RefreshFileList(files[i].getAbsolutePath());
} else
{
filelist.add(files[i].getAbsolutePath());
}
}
}
//过滤当前目录下的指定后缀名的文件
//返回Vector
public Vector<String> FileNameOftype(String strpath2, String filetype)
{
String strfilename = new String();
Vector<String> filelistofType = new Vector<String>();
//读取指定路径下的所有文件
RefreshFileList(strpath2);
for (int i = 0; i < filelist.size(); i++)
{
strfilename = filelist.get(i).toString();
strfilename = strfilename.substring(strfilename.length() - 4,
strfilename.length());
if (strfilename.equals(filetype))
{
filelistofType.add(filelist.get(i));
}
}
return filelistofType;
}
}
本文转自:http://blog.csdn.net/bwgang/article/details/7955964