1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.OutputStreamWriter; import java.nio.channels.FileChannel; public class ChangeFileEncode { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ChangeFileEncode cfe = new ChangeFileEncode(); //아규먼트로 작업할 폴더 경로 설정 //String directoryPath = args[0];//"C:/2014/06"; String directoryPath = "C:/2014/06"; System.out.println("===start==="); cfe.changeFile(directoryPath); System.out.println("===end==="); } private void changeFile(String directoryPath){ String tempFileName = ""; try{ File dirFile = new File(directoryPath); File []fileList = dirFile.listFiles(); //일별 디렉토리 접근 for(File tempFile : fileList) { tempFileName = tempFile.getName(); getFileList(directoryPath+"/"+tempFileName); } }catch(Exception e){ e.printStackTrace(); } } private void getFileList(String directoryPath){ String tempFileName = ""; try{ File dirFile = new File(directoryPath); File []fileList = dirFile.listFiles(); for(File tempFile : fileList) { //파일 가져와 백업 및 변경 if(tempFile.isFile()) { tempFileName = tempFile.getName(); //기존 백업 파일의 백업은 만들지 않는다. if(tempFileName.indexOf("html`back")<0){ //기존 파일뒤에 `back를 붙여서 백업파일 생성 backup(directoryPath+"/"+tempFileName, directoryPath+"/"+tempFileName+"`back"); //백업 받은 파일을 이용하여 UTF-8로 인코딩 변경 encodeUTF8(directoryPath+"/"+tempFileName+"`back" ); } } } }catch(Exception e){ e.printStackTrace(); } } //파일 복사 private void backup(String path, String savePath){ try{ FileInputStream inputStream = new FileInputStream(path); FileOutputStream outputStream = new FileOutputStream(savePath); FileChannel fcin = inputStream.getChannel(); FileChannel fcout = outputStream.getChannel(); long size = fcin.size(); fcin.transferTo(0, size, fcout); fcout.close(); fcin.close(); outputStream.close(); inputStream.close(); }catch(Exception e){ e.printStackTrace(); } } //인코딩 변경 private void encodeUTF8(String path){ String outFilename = path.replaceAll("`back", ""); String s; try { BufferedReader in = new BufferedReader(new FileReader(path)); //기존파일에 엎어쓴다. BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFilename),"UTF-8")); while ((s = in.readLine()) != null) { out.write(s); out.newLine(); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } |
자바 리스트 안에 맵 값 정렬 (java list map value sort) (0) | 2015.03.04 |
---|---|
java submit html form (0) | 2015.02.17 |
Java Collection (Set, List, Map) (0) | 2014.05.22 |
Java 한글깨짐 String Encoding 샘플 (0) | 2014.05.02 |
이클립스(Eclipse)에서 Java8 실행하기 (2) | 2014.03.21 |
댓글 영역