package jp.co.unisys.energy.bems.test.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Decode {
/**
* 字符-->字节 编码
* 字节-->字符 解码
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String str = "中国";
// 编码utf-8
byte[] b = str.getBytes("utf-8");
// 解码US-ASCII
String strDecode = new String(b,"US-ASCII");
// 打印乱码因为编码和解码的字符集不一致
System.out.println(strDecode);
// 必须知道源文件的编码格式
File src = new File("e:/test/b.txt");
decode(src);
}
public static void decode(File src) throws IOException {
// 源文件的编码字符集为unicode
BufferedReader is = new BufferedReader(new InputStreamReader(new FileInputStream(src),"unicode"));
String str;
while(null != (str = is.readLine())) {
System.out.println(new String(str));
}
}
}