我们编程的时候经常遇到这样的问题,我们打开Eclipse就创建了工程开始编辑,但是往往做到一半的时候,被提示按照规定编码格式需要统一为UTF-8。
但是我们的Eclipse默认是选择GBK的,这时候有的人就会一个一个的用其他工具打开,然后粘贴到Eclipse里面替换掉原文件。这样确实不错,但是文件太多时就显得比较麻烦了,而且这也不符合程序员的特点(将一切事情尽可能程序化)。
今天我也遇到了这样的问题,于是简单的写了一个2个java类,实现了该功能,和大家分享一下吧。BUG肯定是有的,但是暂时我还没发现,暂时转化的还都是正常的。
package com.lxl.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.Set;
import com.lxl.helper.IOHelper;
public class Converted {
private static String incode="GBK";//原编码格式
private static String outcode="UTF-8";//将要转化为的编码格式
private static Set<String> typeset;//需要转码的文件后缀
private static String root="E://Test";//工程文件夹
public Converted() {
typeset=new HashSet<String>();
typeset.add("java");//这里进行添加
}
public static void main(String[] args) {
Converted converted = new Converted();
File file=new File(root);
converted.show(file);
}
public void show(File file){
if(file.isDirectory()){
File[] fs = file.listFiles();
for(File f:fs){
show(f);
}
}else{
String name = file.getName();
String[] split = name.split("\\.");
String filename=split[split.length-1];
if(typeset.contains(filename)){
replaceFile(file);
}
}
}
public void replaceFile(File file){
try {
String str = IOHelper.readStrByCode(new FileInputStream(file), incode);
file.delete();
FileOutputStream os = new FileOutputStream(file);
IOHelper.writerStrByCode(os, outcode,str);
os.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
下面就是工具类。
package com.lxl.helper;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class IOHelper {
/**
* 把以一种编码格式的输入流转化为另一种编码格式的输出流
* @return
*/
public static void fromIsToOsByCode(InputStream is,OutputStream os,String incode,String outcode){
BufferedReader reader=null;
BufferedWriter writer=null;
try {
reader = new BufferedReader(new InputStreamReader(is,incode));
writer= new BufferedWriter(new OutputStreamWriter(os, outcode));
String line;
while((line=reader.readLine())!=null){
writer.write(line+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 以指定的格式来读取输入流
*/
public static String readStrByCode(InputStream is,String code){
StringBuilder builder=new StringBuilder();
BufferedReader reader=null;
try {
reader = new BufferedReader(new InputStreamReader(is,code));
String line;
while((line=reader.readLine())!=null){
builder.append(line+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return builder.toString();
}
/**
* 输入InputStream流,返回字符串文字。
* @param is
* @return
*/
public static String fromIputStreamToString(InputStream is){
if(is==null)return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
try {
while ((i = is.read()) != -1) {
baos.write(i);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return baos.toString();
}
/**
* 输入InputStream流和文件地址,返回成功与否。
* @param is
* @return
*/
public static boolean fromIputStreamToFile(InputStream is,String outfilepath){
byte[] b=new byte[1024];
FileOutputStream fos=null;
try {
fos=new FileOutputStream(new File(outfilepath));
while((is.read(b, 0, 1024))!=-1){
fos.write(b);
}
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
/**
* 输入文件地址,返回inputStream流。
* @param is
* @return
*/
public static InputStream fromFileToIputStream(String infilepath){
FileInputStream fis=null;
try {
fis=new FileInputStream(new File(infilepath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fis;
}
public static InputStream fromStringToIputStream(String s) {
if (s != null && !s.equals("")) {
try {
ByteArrayInputStream stringInputStream = new ByteArrayInputStream(
s.getBytes());
return stringInputStream;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 把输入流转化成UTF-8格式的输入流
* @param in
* @param charset
* @return
* @throws Exception
*/
// public static InputStream fromInputStreamToInputStreamInCharset(
// InputStream in, String charset) throws Exception {
// StringBuilder builder=new StringBuilder();
// byte[] buffer = new byte[2048];
// int len = -1;
// while ((len = in.read(buffer)) != -1) {
// builder.append(EncodingUtils.getString(buffer, 0, len, "UTF-8"));
// }
// return IOHelper.fromStringToIputStream(builder.toString());
// }
public static InputStream getInputStreamFromUrl(String urlstr){
try {
InputStream is = null;
HttpURLConnection conn = null;
System.out.println("urlstr:"+urlstr);
URL url = new URL(urlstr);
conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == 200) {
is = conn.getInputStream();
return is;
}
} catch (Exception e) {
System.out.println(e.toString());
}
return null;
}
public static void writerStrByCode(FileOutputStream os,
String outcode,String str) {
BufferedWriter writer=null;
try {
writer= new BufferedWriter(new OutputStreamWriter(os, outcode));
writer.write(str);
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}