1. 程式人生 > >JavaSE8基礎 String 對字符串進行編碼 對字節數組進行解碼

JavaSE8基礎 String 對字符串進行編碼 對字節數組進行解碼

utf-8 block rac news bsp 手冊 try lease pre



os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)



code:

package jizuiku0;

import java.io.UnsupportedEncodingException;

/*
 * @version V17.09
 */
public class CodeDemo {
	public static void main(String[] args) {
		// 指定要使用的字符集的名字,使用 UTF-8字符集
		String myCharset = "UTF-8";
		String str = "博客園";
		byte[] bs;

		try {
			// 根據指定的字符集 對字符串進行編碼
			bs = str.getBytes(myCharset);

			// 根據指定的字符集 對字符串進行解碼
			String newStr = new String(bs, myCharset);
			System.out.println(newStr);

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


result:
技術分享


Java優秀,值得學習。
學習資源:API手冊 + Java源碼 + 清凈的心地。

JavaSE8基礎 String 對字符串進行編碼 對字節數組進行解碼