字符编码转换

    技术2022-05-12  46

    import java.io.ByteArrayInputStream;   import java.io.FileInputStream;   import java.io.IOException;   import java.io.InputStream;   import java.io.InputStreamReader;   import java.io.Reader;     public class EncodingConverter {         public static final String UTF_8 = "UTF-8";              public static InputStream converter(InputStream is, String srcEnc, String tarEnc)               throws IOException {             // builder a reader using source encoding           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             // get byte array           InputStream tarIs = new ByteArrayInputStream(sb.toString().getBytes(tarEnc));           return tarIs;       }         public static InputStream converterToUTF(InputStream is, String srcEnc)               throws IOException {             // builder a reader using source encoding           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             // get byte array           InputStream tarIs = new ByteArrayInputStream(sb.toString().getBytes("utf-8"));           return tarIs;       }         public static byte[] converterToByteArray(InputStream is, String srcEnc, String tarEnc)               throws IOException {             // builder a reader using source encoding           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             // get byte array           return sb.toString().getBytes(tarEnc);       }         public static byte[] converterToUTFByteArray(InputStream is, String srcEnc)               throws IOException {             // builder a reader using source encoding           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             // get byte array           return sb.toString().getBytes("utf-8");       }         public static String converterToString(InputStream is, String srcEnc, String tarEnc)               throws IOException {             // builder a reader using source encoding           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             return sb.toString();       }         public static String converterToUTFString(InputStream is, String srcEnc)               throws IOException {             // builder a reader using source encoding           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             return sb.toString();       }         public static InputStream converter(byte[] bytes, String srcEnc, String tarEnc)               throws IOException {             // builder a reader using source encoding           InputStream is = new ByteArrayInputStream(bytes);           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             // get byte array           InputStream tarIs = new ByteArrayInputStream(sb.toString().getBytes(tarEnc));           return tarIs;       }         public static InputStream converterToUTF(byte[] bytes, String srcEnc)               throws IOException {             // builder a reader using source encoding           InputStream is = new ByteArrayInputStream(bytes);           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             // get byte array           InputStream tarIs = new ByteArrayInputStream(sb.toString().getBytes("utf-8"));           return tarIs;       }         public static byte[] converterToByteArray(byte[] bytes, String srcEnc, String tarEnc)               throws IOException {             // builder a reader using source encoding           InputStream is = new ByteArrayInputStream(bytes);           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             // get byte array           return sb.toString().getBytes(tarEnc);       }         public static byte[] converterToUTFByteArray(byte[] bytes, String srcEnc)               throws IOException {             // builder a reader using source encoding           InputStream is = new ByteArrayInputStream(bytes);           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             // get byte array           return sb.toString().getBytes("utf-8");       }         public static String converterToString(byte[] bytes, String srcEnc, String tarEnc)               throws IOException {             // builder a reader using source encoding           InputStream is = new ByteArrayInputStream(bytes);           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             return sb.toString();       }         public static String converterToUTFString(byte[] bytes, String srcEnc)               throws IOException {             // builder a reader using source encoding           InputStream is = new ByteArrayInputStream(bytes);           Reader reader = new InputStreamReader(is, srcEnc);           StringBuilder sb = new StringBuilder();             // read content           int c;           while ((c = reader.read()) != -1) {               sb.append((char) c);           }           reader.close();             return sb.toString();       }       public static void main(String[] args) throws IOException {           InputStream is = new FileInputStream("G:/out.txt");           is = converter(is, "utf-8", "gbk");           Reader isr = new InputStreamReader(is, "gbk");           StringBuilder sb = new StringBuilder();           int c;           while ((c = isr.read()) != -1) {               sb.append((char) c);           }           is.close();           System.out.println(sb.toString());       }   } 


    最新回复(0)