黑马程序员_Java_Properties对象和IO中的其他对象

-------android培训java培训、期待与您交流! ----------

 

1.Properties类是Hashtable的子类。他具备了Map集合的特点,而且他里面存储键值对都是字符串。是集合中和IO技术相结合的容器,可以用于键值对形式的配置文件的操作。

我们可以直接调用load方法加载一个字节输入流,该流包装了相应的键值对形式的文本文件。加载完成后Properties对象内部就将文本中的键值内容映射成了Map集合。可以调用Map集合的相关的方法来操作该properties对象。调用store方法可以将properties对象中的内容更新到指定配置文件中。同时具备添加注释的功能。

2.IO中的打印流:PrintStream(字节),PrintWriter(字符),可以直接操作输入流和文件。

该流提供了打印方法,可以将各种数据类型的数据都原样打印。通常我们使用的System.out.println()。使用的就是PrintStream的打印的方法。

PrintStream能够接受的构造函数参数类型:

1)file对象。File

2)字符串路径。String

3)字节输出流。OutputStream

PrintWriter能够接受的构造函数类型比PrintStream多一个:字符输出流Writer。

3.SequenceInputStream序列流对多个流进行合并。

示例:

//分割一个文件到多个文件中

public static void splitFile()throws IOException
	{
		FileInputStream fis = new FileInputStream("c:\\1.bmp");
		FileOutputStream fos = null;
		byte[] buf = new byte[1024*1024];
		int len=0;
		int count=1;
		while((len = fis.read(buf))!=-1)
		{
			fos = new FileOutputStream("C:\\splitfiles\\"+(count++)+".part");
			fos.write(buf,0,len);
			fos.close();
		}
		fis.close();	
	} 


//将多个文件合并到一个文件中

public static void merge()
	{
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
		for(int x=1; x<=3; x++)
		{
			al.add(new FileInputStream("c:\\splitfiles\\"+x+".part"));
		}
		
		final Iterator<FileInputStream> it=al.iterator();
		Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
		{
			public boolean hasMoreElements()
			{
				return it.hasNext();
			}
			public FileInputStream nextElement()
			{
				return it.next();
			}
		};
		
		SequenceInputStream sis= new SequenceInputStream(en);
		FileOutputStream fos = new FileOutputStream("c:\\splitfiles\\0.bmp");
		byte[] buf=new byte[1024];
		int len=0;
		while((len=sis.read(buf))!=-1)
		{
			fos.write(buf,0,len);
		}
		fos.close();
		sis.close();		
	}

 

 

-------android培训java培训、期待与您交流! ----------

详细请查看:http://edu.csdn.net/heima/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值