几种常见编码如下:
import java.io.UnsupportedEncodingException;
public class CoreEncoder {
public static void main(String[] args) throws Exception {
String str = "就这样YWD";
byte[] byte1 = str.getBytes();
System.out.print("默认GBK :");
for(byte b:byte1){
//把字节转换成int以16进制方式表现出来;
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
byte[] byte2 = str.getBytes("gbk");
System.out.print("GBK :");
for(byte b:byte2){
//设置编码为gbk输出
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
byte[] byte3 = str.getBytes("utf-8");
System.out.print("utf-8 :");
for(byte b:byte3){
//设置编码为utf-8输出
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
byte[] byte4 = str.getBytes("utf-16be");
System.out.print("utf-16be:");
for(byte b:byte4){
//设置编码为gbk输出
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
String str1 = new String(byte4);//用项目默认编码
System.out.println(str1);
String str2 = new String(byte4,"utf-16be");//用对应编码编码
System.out.println(str2);
}
/*
* 总结:gbk编码中文两个字节,英文一个;
* utf-8编码中文三个字节,英文一个;
* utf-16be中英文都是两个字节;
* 当你字节序列是某种编码时,想要将其变成字符串也必须用这种编码;
*/
}
看以上注释,不用多解释了。
文件内容编码转换
public class IOCVUtils {
/** 源文件编码 */
public static String sourceEncoding = "GBK";
/** 目标编码 */
public static String targetEncoding = "UTF-8";
/**
* 文件内容转编码
* @param sourceFile
* @param targetFile
* @throws UnsupportedEncodingException
* @throws FileNotFoundException
* @throws IOException
*/
public static void changeEncoding(File sourceFile, File targetFile)
throws UnsupportedEncodingException, FileNotFoundException,
IOException {
FileInputStream fin = null;
FileOutputStream fout = null;
FileChannel fcin = null;
FileChannel fcout = null;
if (sourceEncoding == null) {
IOCVUtils.sourceEncoding = System.getProperty("file.encoding");
}
try {
fin = new FileInputStream(sourceFile);
fout = new FileOutputStream(targetFile);
fcin = fin.getChannel();
fcout = fout.getChannel();
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while (true) {
buffer.clear();
int r = fcin.read(buffer);
if (r == -1) {
break;
}
buffer.flip();
fcout.write(ByteBuffer.wrap(Charset.forName(sourceEncoding).decode(buffer).toString().getBytes(targetEncoding)));
}
} finally {
if (fin != null) {
fin.close();
fin = null;
}
if (fcin != null) {
fcin.close();
fcin = null;
}
if (fout != null) {
fout.close();
fout = null;
}
if (fcout != null) {
fcout.close();
fcout = null;
}
}
}
/**
* 文件内容转编码
* @param sourceFile
* @param targetFile
* @throws UnsupportedEncodingException
* @throws FileNotFoundException
* @throws IOException
*/
public static void changeEncoding(String sourceFile, String targetFile) throws UnsupportedEncodingException, FileNotFoundException, IOException{
File fl1 = new File(sourceFile);
File fo1 = new File(targetFile);
changeEncoding(fl1, fo1);
}
/**
* 文件内容转编码
* @param sourceFile
* @param targetFile
* @param sourceEncoding 源文件编码 默认源文件的系统存储编码 System.getProperty("file.encoding");
* @param targetEncoding 目标编码 默认utf-8
* @throws UnsupportedEncodingException
* @throws FileNotFoundException
* @throws IOException
*/
public static void changeEncoding(String sourceFile, String targetFile,
String sourceEncoding, String targetEncoding) throws UnsupportedEncodingException, FileNotFoundException, IOException {
IOCVUtils.sourceEncoding = sourceEncoding;
IOCVUtils.targetEncoding = targetEncoding;
changeEncoding(sourceFile, targetFile);
}
}