import java.io.*;
public class EncodeStrem {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//writeText();
readText();
}
public static void readText() throws IOException
{
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");
char[] buf = new char[6];
isr.read(buf);
String s=new String(buf);
System.out.println(s);
}
public static void writeText() throws IOException
{
OutputStreamWriter osw=
new OutputStreamWriter(new FileOutputStream("utf.txt"),"utf-8");
osw.write("你好");
osw.close();
}
}
java中编码解码的实例
import java.io.*;
import java.util.*;
public class EncodeDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String s= "你好";
byte[] b= s.getBytes("utf-8");//编码过程
// String s1= new String(b,"gbk");//解码过程
// System.out.println(s1);
//System.out.println(Arrays.toString(b));
String s3 = new String(b,"ISO8859-1");//进行了错误的解码后
System.out.println(s3);
byte[] b2 = s3.getBytes("ISO8859-1");//进行一次员解码的编码得到原来的字节
System.out.println(Arrays.toString(b2));
String s2 = new String(b2,"utf-8");
System.out.println(s2);
//注意:如果用UTF-8是不适用这种方法的
}
}