功能演示:将一个文件分割成多个100KB以下的小碎片,再进行还原文件
- /**
- * 分割文件流
- * 将一个图片分割成100kb一个的小文件
- * @throws Exception
- * @since JDK 1.6
- */
- public void spiltFile() throws Exception{
- FileInputStream fis = new FileInputStream("C:\\myEclipse.jpg");
-
- FileOutputStream fos = null;
- int len = 0;
- byte[] bf = new byte[102400];
- int count = 1;
-
- while((len = fis.read(bf)) != -1 ){
- fos = new FileOutputStream("c:\\spiltFile\\"+count+".part");
- count++;
- fos.write(bf, 0, len);
- fos.close();
- }
- fis.close();
-
- }
-
- /**
- * 功能流SequenceInputStream功能演示 功能,可以将多个流放到一起进行合流
- * 程序功能:将spiltFile文件夹的碎片文件合并到test.jpg
- * @throws Exception
- * @since JDK 1.6
- */
- public void suquenceDemo() throws Exception {
- Vector<InputStream> v = new Vector<InputStream>();
- File files = new File("c:\\spiltFile");
- for (File f : files.listFiles()) {
- v.add(new FileInputStream(f));
- }
- Enumeration<InputStream> en = v.elements();
- SequenceInputStream sis = new SequenceInputStream(en);
- FileOutputStream fos = new FileOutputStream("c:\\spiltFile\\Eclipse.jpg");
- int len = 0;
- byte[] bf = new byte[1024];
- while ((len = sis.read(bf)) != -1) {
- fos.write(bf, 0, len);
- }
- fos.close();
- sis.close();
- }
复制代码
|