개발 과정에서 파일 인코딩 변환이 발생할 수 있습니다. 개발 도구 이클립스는 인코딩을 변환 할 수 있지만 경우에 따라 불편합니다. 예를 들어, 파일 자체의 원래 코드는 GBK이지만 이제 UTF-8로 변환해야합니다. 파일 인코딩이 Eclipse에서 UTF-8으로 직접 수정 된 경우 축하합니다. GBK에서 UTF-8로 직접 변환 할 수 없으므로 인코딩을 수동으로 변환해야합니다. 아래는 변환 도구 클래스를 인코딩하는 파일입니다.
패키지 com.mikan.stuff; import java.io.file; import java.io.fileInputStream; import java.io.fileoutputStream; import java.io.filenamefilter; import java.io.inputstream; import java.io.inputStreamReader; import java.io.outputStream; import java.io.outputStreamwriter; import java.nio.charset.charset; import java.nio.charset.unsupportedcharsetexception; 공개 클래스 FILECHARSETCONVERTER {public static void main (String [] args)은 예외 {conver { "d : //stuff//src//main//java/com/mikan//stuff//test.txt", "gbk", "utf-8", New Filename Filter () { @override volean 수락 (파일) name.endswith ( "txt"); } / ** * 지정된 파일 또는 디렉토리를 지정된 인코딩 * * @param filename * 파일로 변환 할 * @param fromchcharsetName * 인코딩 * @param tocharsetName * 인코딩 * @Throws Exception * / public static void convert (string filename, string to toprows {convertname)). CharchsetName, ToCharsetName, null); } / ** * 지정된 파일 또는 디렉토리를 지정된 인코딩 * * @param 파일 또는 디렉토리로 변환 할 수 있습니다 * @param fromChareTname * @param fromschcharsetName * @param tocharsetName * 인코딩 * @throws Exception * / public static void (파일, 문자열 to ThanchsetName) TocharsetName, null); } / ** * 지정된 파일 또는 디렉토리를 지정된 인코딩 * * @param 파일 * 파일 또는 디렉토리로 변환하여 소스 파일의 인코딩 * @param tocharsetname * 인코딩 * @param 필터 * 파일 이름 필터 * / public void convert (filename, string, string, string, string, string, string, string, string, string) FilenameFilter Filter)는 Exception {Convert (새 파일 (filename), fromcharchsetName, toCharsetName, 필터); } / ** * 지정된 파일 또는 디렉토리를 지정된 인코딩 * * @param 파일 * 파일 또는 디렉토리로 변환 할 수 있습니다. filenamefilter filter)는 예외 {if (file.isdirectory ()) {file [] filleList = null; if (filter == null) {filelist = file.listfiles (); } else {filelist = file.listfiles (필터); } for (file f : fileList) {convert (f, fromcharchsetName, toCharSetName, 필터); }} else {if (filter == null || filter.accept (file.getParentFile (), file.getName ())) {String fileContent = getFileContentFromCharset (file, fromcharchetName); savefile2charset (file, tocharsetname, filecontent); }}} / ** * 지정된 인코딩에서 파일을 읽고 파일 내용을 읽고 변환 할 파일 * @param 파일 * 파일을 변환 할 * @param fromchcharsetName * 인코딩 * @return * @Throws Exception * / public static string getFileContentFromCharSet (파일) {charsepported (hrounsupported) (wromesupported) UnsupportedCharseTexception (FromcharsetName); } inputStream inputStream = 새로운 fileInputStream (파일); inputStreamReader reader = 새 inputStreamReader (inputStream, fromcharchsetName); char [] chs = new char [(int) file.length ()]; reader.read (chs); String str = new String (chs) .trim (); reader.close (); Return str; } / ** * 지정된 인코딩 메소드에서 텍스트 파일을 쓰기 * @param 파일 * 파일을 덮어 쓰기 * @param tocharsetname * 인코딩 * @param content * 파일 content * @throws exception * / public static void savefile2charset (file file, string to CharchsetName) 예외 {! charset.isset (to is supported)). 새로운 UnsupportedCharsetexception을 던지십시오 (ToCharsetName); } outputStream outputStream = 새 FileOutputStream (파일); outputStreamWriter outWrite = new OutputStreamWriter (outputStream, tocharsetName); untwrite.write (컨텐츠); untwrite.close (); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.