Java IO 之字符集相关及文件合并

一、中文相关的字符集

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() ;
	}
}
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值