In the course of development,

may encounter the conversion of file encoding. Although the development tool eclipse can transform encoding, some cases are very inconvenient. For example, the original file itself code is GBK, now to convert to UTF-8, if directly in the eclipse to modify the file encoding into UTF-8, Congratulations, is a random code, because you can not directly from GBK to UTF-8 conversion, then we need to manually convert the encoding. The following is a tool class for file encoding conversion.

 package com.mikan.stuff; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter. Java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; public class FileCharsetConverter {public static void main (String[] args) {@Override public Boolean accept (File dir, String name) {return name.endsWith ("TXT");}});} / * * * convert the specified file or directory into a specified encoding * * @param paste to convert. The encoding of the etName * source file * @param toCharsetName * the code to convert * @throws Exception * / public static void convert (String fileName. RsetName, null);} / * * convert the specified file or directory into the specified encoding * @param file * file or directory * the encoding of the @param fromCharsetName * source file * @param toCharsetName * encoding * @throws Exception * / Public static void convert (File file, String fromCharsetName, String toCharsetName) throws Exception {} / * * convert the specified file or directory into a specified encoding * * * file or directory to be transferred. * the encoding of the @param fromCharsetName * source file * @param toCharsetName * encoding * @param filter * filename filter * @throws Exception * / public static void convert IlenameFilter filter) throws Exception {convert (New File (fileName), fromCharsetName, toCharsetName, filter);} / * * converting the specified file or directory into a specified encoding * * @param file or directory * source file Encoding * @param toCharsetName * encoding * @param filter * filename filter * @throws Exception * / public static void convert Rectory ()) {File[] fileList = null; if (filter = = null) {fileList = file.listFiles ();} else {fileList = file.listFiles;}} {} {} Filter = null filter.accept (file.getParentFile () (), file.getName ())) {String fileContent = getFileContentFromCharset (file, fromCharsetName); saveFile2Charset (saveFile2Charset,})}}} / * * to be coded Method reads the file, returns the file content * @param file * the encoding of the file * @param fromCharsetName * source file * @return * @throws Exception * / public static String getFileContentFromCharset Ception {if (! Charset.isSupported (fromCharsetName)) {throw new UnsupportedCharsetException (fromCharsetName);} InputStream inputStream = new FileInputStream. New char[(int) file.length ()]; reader.read (CHS); String STR = new String (CHS).Trim ();} / * * * * * * * / * * to write a text file in a specified encoding method. The encoding * @param content * file content * @throws Exception * / public static void saveFile2Charset (File file, String toCharsetName) Ption (toCharsetName);} OutputStream outputStream = new FileOutputStream (file); OutputStreamWriter outWrite = new OutputStreamWriter (outputStream,); Hope to help everyone learn, and I hope you will support the script home.