Selama proses pengembangan, Anda dapat menghadapi konversi pengkodean file. Meskipun gerhana pengembangan Eclipse dapat mengonversi pengkodean, itu tidak nyaman dalam beberapa kasus. Misalnya, kode asli file itu sendiri adalah GBK, tetapi sekarang perlu dikonversi ke UTF-8. Jika pengkodean file dimodifikasi secara langsung menjadi UTF-8 di Eclipse, selamat, itu kacau, karena tidak dapat secara langsung dikonversi dari GBK ke UTF-8, kita perlu mengonversi pengkodean secara manual. Di bawah ini adalah kelas alat konversi pengkodean file.
paket com.mikan.stuff; impor java.io.file; impor java.io.fileInputStream; impor java.io.fileoutputStream; impor java.io.filenamefilter; impor java.io.inputstream; impor java.io.inputStreamReader; impor java.io.outputStream; impor java.io.outputStreamWriter; impor java.nio.charset.charset; impor java.nio.charset.unsupportedCharsetException; public class FileCharsetConverter { public static void main(String[] args) throws Exception { convert("D://stuff//src//main//java//com/mikan//stuff//test.txt", "GBK", "UTF-8", new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endswith ("txt"); } /** * Convert the specified file or directory to the specified encoding* * @param fileName * File to be converted* @param fromCharsetName * Encoding of the source file* @param toCharsetName * Encoding to be converted* @throws Exception */ public static void convert(String fileName, String fromCharsetName, String toCharsetName) throws Exception { convert(new File (nama file), fromCharsetName, tocharsetname, null); } / ** * Konversi file atau direktori yang ditentukan ke pengkodean yang ditentukan * * @param file * FromCharsetName, TocharsetName, null); } / ** * Konversi file atau direktori yang ditentukan ke pengkodean yang ditentukan * * @param file * FileNamefilter Filter) melempar Exception {Convert (file baru (fileName), FromCharSetName, ToCharSetName, filter); } / ** * Konversi file atau direktori yang ditentukan ke pengkodean yang ditentukan * * file @param * FileNamefilter Filter) melempar Exception {if (file.isDirectory ()) {file [] fileList = null; if (filter == null) {fileList = file.listFiles (); } else {fileList = file.listFiles (filter); } untuk (file f: filelist) {convert (f, fromCharsetName, tocharsetName, filter); }} else {if (filter == null || filter.accept (file.getParentFile (), file.getName ())) {string filecontent = getFileContentFromCharset (file, fromCharsetName); saveFile2charset (file, tocharsetname, filecontent); } } } /** * Read the file in the specified encoding and return the file content* * @param file * File to be converted* @param fromCharsetName * Encoding of the source file* @return * @throws Exception */ public static String getFileContentFromCharset(File file, String fromCharsetName) throws Exception { if (!Charset.isSupported(fromCharsetName)) { throw New UnsupportedCharsException (FromCharsetName); } InputStream inputStream = FileInputStream baru (file); InputStreamReader reader = inputStreamReader baru (inputStream, fromCharsetName); char [] chs = char baru [(int) file.length ()]; reader.read (CHS); String str = string baru (chs) .trim (); reader.close (); mengembalikan str; } /** * Write text file in a specified encoding method, existence will overwrite* * @param file * File to be written* @param toCharsetName * Encoding to be converted* @param content * File content* @throws Exception */ public static void saveFile2Charset(File file, String toCharsetName, String content) throws Exception { if (!Charset.isSupported(toCharsetName)) {Throw New UnsupportedCharSetException (TocharsetName); } OutputStream outputStream = new fileoutputStream (file); OutputStreamWriter outwrite = outputStreamWriter baru (outputStream, toCharsetName); outwrite.write (konten); outwrite.close (); }}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.