java做编码转换有两中方法
1.基于流的编码转换
InputStream is = Main.class.getResourceAsStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String tmp;
while((tmp = br.readLine()) != null){
System.out.println(tmp);
}
2.基于字符的编码转换
InputStream is = Main.class.getResourceAsStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String tmp;
while((tmp = br.readLine()) != null){
System.out.println(tmp);
}
我这里用的是第一种方法,其他不多说了直接贴代码了
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());
}
}