자바 파일 인 코딩 변경

5860 단어
직접 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TransCoding {
//        
private static String path = "C:\\Users\\Administrator\\Desktop\\123\\HelpUDcAppBG";
//      。    (            , ”“,   null)
//                ,         
private static String directory = "C:\\Users\\Administrator\\Desktop\\12";
//         。    (            , ”“,   null)
//              ,         。
private static String typeName = "";
//         ,  GBK。          ANSI,     GBK  。
private static String codeBefore = "GB2312";
//         ,  UTF-8。
//                   ,    ,        UTF-8,
//          UTF-8  ,   windows     ,         。
private static String codeAfter = "UTF-8";

public static void main(String[] args){
    File dir = new File(path);
    File dirPath = new File(directory);
    change(dir,dirPath,typeName);

}

public static void change(File dir,File dirPath,final String typeName){
   BufferedReader bufferedReader = null;
   BufferedWriter bufferedWriter =null;
    //                 ,    。
   File[]   files = dir.listFiles(new FilenameFilter(){
        public boolean accept(File dir,String name){
        //     ,    true,         listFiles       。
            if(new File(dir,name).isDirectory()){
                return true;
            };
            return name.endsWith(typeName);
        }
    });

    for (File file : files) {
        //           ,           
        String pathAbsolute = null;
        //           ,              
        File fileModify = null;
        //        
        File createDir = null;
        //            
        File createFile = null;
        //    file       ,      ,  。
        if(file.isDirectory()){
            //         
            String path = file.getAbsolutePath();
            //      
            String path2 = path.substring(0,path.lastIndexOf("\\"));
            //       
            String path3 = path2.substring(path2.lastIndexOf("\\"));
            //         。
            File file2 =null;
            if(dirPath.getName()==null||dirPath.getName().trim()==""){
                change(file,dirPath,typeName);
            }else{
                file2 = new File(dirPath,path3);
                change(file,file2,typeName);
            }

        }else{//    ,    
         try {
             //       ,        。
            bufferedReader = new BufferedReader(new InputStreamReader(
                new FileInputStream(file.getAbsolutePath()),codeBefore));
            //        ,         。
            if(dirPath.getName()==null||dirPath.getName()==""){
                //        D:\action\1.txt
                  pathAbsolute = file.getAbsolutePath();
                //        D:\action\
                String path1 = pathAbsolute.substring(0,
                    pathAbsolute.lastIndexOf(file.getName()));
                //      D:\action\11.txt
                String pathModify = path1+"1"+file.getName();
                fileModify = new File(pathModify);
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileModify),codeAfter));
            }else{//  ,              

                String path = file.getAbsolutePath();
                String fileName = file.getName();
                //             
                String path2 = file.getAbsolutePath().substring(0,
                    path.lastIndexOf(fileName)-1);
                //            
                String path3 = path2.substring(path2.lastIndexOf("\\"));
                //      
                 createDir = new File(dirPath,path3);
                //         ,       (      .java .txt),
                //         ,        ,  IO    。
                createDir.mkdirs();
                createFile = new File(createDir,fileName);
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(createFile),codeAfter));
            }
            String line = null;
            while((line=bufferedReader.readLine())!=null){
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }
            //           ,            。
            //    while        ,      。
            //               
            bufferedWriter.flush();

            } catch (Exception e) {
                if(createDir!=null)
                    createDir.deleteOnExit();
                if(createFile!=null)
                    createFile.deleteOnExit();
                throw new RuntimeException("    "+e);
            } 
             finally{
                 if(bufferedReader!=null){
                     try {
                        bufferedReader.close();

                    } catch (IOException e) {
                        throw new RuntimeException("       ");
                    }
                 }
                 if(bufferedWriter!=null){
                     try {
                        bufferedWriter.close();
                        if(fileModify!=null){
                            //      
                            file.delete();
                            //           File  
                            File file3 = new File(pathAbsolute);
                            //              
                            fileModify.renameTo(file3);
                            }
                    } catch (IOException e) {
                        throw new RuntimeException("       ");
                    }
                 }
            }

         }
     }
 }

}

좋은 웹페이지 즐겨찾기