将jbk格式的文件转换成utf-8的

来源:互联网 发布:百度网盘mac版是什么 编辑:程序博客网 时间:2024/05/16 02:11

今天从网上下载了一个项目,里面的java文件都是用jbk编写的。而我的myeclipse默认的打开方式是utf-8。而且我的项目也是用utf-8打开的。所以就打算吧jbk的格式改掉。但是文件太多,一个一个的改太麻烦了。所以就写了一个程序:代码如下:

package util;

import java.io.*;

public class ChangeToUtf8
{

    public static void main(String[] args) throws IOException
    {
        System.out.println("请输入文件目录");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String fileName=br.readLine();
        File file = new File(fileName);
        new ChangeToUtf8().changeFile(file);
    }
    
    public void changeFile(File file){
        CharArrayWriter bis = null;
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        char[] tempchars = new char[20];
        int byteread;
        try {
            if(file.isDirectory()){
                System.out.println(file.toString()+"这个目录正在处理");
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    changeFile(files[i]);
                }
            }else {

                System.out.println(file.toString()+"这个文件正在处理");
                byteread = 0;
                bis = new CharArrayWriter();
                if(!isUtf8(file)){
                    isr = new InputStreamReader(new FileInputStream(file), "gb2312");
                }else{                    
                    return;
                }
                
                while ((byteread = isr.read(tempchars)) != -1) {
                    bis.write(tempchars, 0, byteread);                
                }
                String s = new String(bis.toCharArray());
                osw =new OutputStreamWriter(new FileOutputStream(file), "utf-8");               
                osw.write(s);
                osw.flush();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (isr != null){                
              try
            {
                isr.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }               
            }
            if(osw != null){
                try
                {
                    osw.close();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if(bis != null){
                bis.close();
            }
        }
    }
    
    public boolean isUtf8(File f){
        InputStream ios = null;
        try{  
           ios=new FileInputStream(f);  
           byte[] b=new byte[3];  
           ios.read(b);  
           ios.close();  
           if(b[0]==-17&&b[1]==-69&&b[2]==-65){
               System.out.println(f.getName()+"编码为UTF-8");
               return true;
           }
           else{
               System.out.println(f.getName()+"可能是GBK");
               return false;
           }
        }catch(Exception e){  
           e.printStackTrace();  
        }finally{
            if (ios != null){                
                  try
                {
                    ios.close();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }               
                }
        }
        return false;        
    }
}

但是在用的时候发现了一个问题。就是说有的xml文件无法分辨去他原来的格式。所以用的时候先把要改变的文件备份起来,以防万一。

原创粉丝点击