一、中文相关的字符集
GBK:包含繁体和简体的字符集
GB2312:主要指简体中文
ISO8859-1:国际通用编码
JVM中默认的编码方式是:GBK
乱码产生的根本原因是:两个操作间的字符集没有统一起来。
二、查看当前系统的编码方式
System:public static Properties getProperties()确定当前的系统属性。
Properties:public void list(PrintStream out)将属性列表输出到指定的输出流。此方法对调试很有用。
public class ShowPropertiesDemo
{
public static void main(String args[])
{
// 通过此代码观察一下当前JVM中设置的属性
System.getProperties().list(System.out) ;
}
}
三、将信息以指定编码存入文件
import java.io.* ;
public class EncodeDemo
{
public static void main(String args[]) throws Exception
{
OutputStream out = null ;
out = new FileOutputStream(new File("D:/FileTest/a.txt")) ;
String str = "Hello,World" ;
out.write(str.getBytes("GB2312")) ;
out.close() ;
}
};
四、SequenceInputStream类及其应用
SequenceInputStream:public SequenceInputStream(InputStream s1,InputStream s2)通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),以提供从此 SequenceInputStream 读取的字节。
import java.io.* ;
public class SequenceInputStreamDemo{
public static void main(String args[]) throws Exception{
InputStream in1 = null ;
InputStream in2 = null ;
// 建立一个输出流
OutputStream out = null ;
in1 = new FileInputStream(new File("C:\\Users\\HP\\Desktop\\明年.txt")) ;
in2 = new FileInputStream(new File("C:\\Users\\HP\\Desktop\\今天.txt")) ;
out = new FileOutputStream(new File("C:\\Users\\HP\\Desktop\\hebing.txt")) ;
// 此处相当于将两个文件合并了
SequenceInputStream seq = null ;
seq = new SequenceInputStream(in1,in2) ;
// 文件合并之后输出到:lxhmldn.txt文件之中
int c = 0 ;
while((c=seq.read())!=-1)
{
out.write(c) ;
}
in1.close() ;
in2.close() ;
out.close() ;
seq.close() ;
}
}