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

来源:互联网 发布:西安智汇诚网络怎么样 编辑:程序博客网 时间:2024/05/22 14:36

-------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/