java基础—文件的切割与合并


java中文件的切割与合并操作


文件的切割 

	/**
	 * 
	 * @param file		要分割的文件 
	 * @param paths		要分割的文件保存的路径
	 * @param format	要分割的文件保存的格式 
	 * @throws Exception
	 */
	public static void spilt(File file,String paths,String format) throws Exception  
	{  

		System.out.println("分割文件功能启动");  
		//将文件封装对象  
		//使用流关联文件对象  
		FileInputStream fis = new FileInputStream(file);  
		String name = file.getName();  
		//定义一个缓冲区  
		byte[] byt = new byte[1024*1024];  
		//创建一个目的地  
		FileOutputStream fos = null;  
		//设置分割文件储存路径  
		File fi = new File(paths);  
		//创建存储分割文件的文件夹,如果目的文件夹不存在,那么就创建  
		if(!fi.exists())  
			fi.mkdirs();  
		int len = 0;  
		int count = 1;  
		//创建一个储存数据的容器  
		Properties pro = new Properties();  
		//读取文件数据  
		while((len=fis.read(byt))!=-1)  
		{  
			//将文件命名并添加到流当中  
			fos = new FileOutputStream(new File(fi,name+(count++)+format));  
			fos.write(byt,0,len);  
			fos.close();  
		}  
		//将被切割的文件保存到pro集合中去  
		pro.setProperty( "partcount",count + "" );  
		pro.setProperty( "filename",file.getName());  

		fos = new FileOutputStream(new File(fi,count + ".properties" ));  

		//将prop集合中的数据存储到文件中  
		pro.store(fos, "save file info");  

		fis.close();  
		fos.close();  

	}  


要合并文件 的方法 

/**
	 * 
	 * @param file		要合并的文件 
	 * @param namess	要合并的文件保存的名字
	 * @param paths		要合并的文件保存的路径
	 * @param format	要合并后保存的文件格式
	 * @throws Exception
	 */
	public static void megrefile(File file,String namess,String paths,String format) throws Exception
	{
		System.out.println("合并文件功能启动");
		//获取指定目录下的文件以及指定格式的文件
		File[] files = file.listFiles(new Merger(format));
		//创建一个容器用来储存这些文件流对象
		Vector<FileInputStream> v = new Vector<FileInputStream>();
		for(int x=0;x<files.length;x++)
		{
			//将获取的文件与流关联并添加到容器中去
			v.add(new FileInputStream(files[x]));
		}
		Enumeration<FileInputStream> en =v.elements();
		//合并多个流
		SequenceInputStream sis = new SequenceInputStream(en);
		//创建输出文件目录
		FileOutputStream fos = new FileOutputStream(paths+namess+format);
		int len = 0;
		byte[] byt = new byte[1024];
		while((len=sis.read(byt))!=-1)
		{
			fos.write(byt);
			fos.flush();
		}
		sis.close();

	}


在使用的时候,可以先将一个文件进行切割,然后再进行合并 

		//获取将需要切割的文件
		File  file = new File("E:\\myjava\\2015-6-14\\凤凰传奇-最炫民族风.mp36quot");
		//调用自定义切割文件功能
		spilt(file,"E:\\myjava\\2015-6-14\\abc",".mp3");
		//获取需要合并文件的目录
		File files = new File("E:\\myjava\\2015-6-14\\abc");
		//调用自定义合并文件功能
		megrefile(files,"myis","E:\\myjava\\2015-6-14\\121\\",".mp3");


	//设置一个筛选指定格式文件的过渡器  
	public class Merger implements FilenameFilter  
	{  
		private String format;  
		Merger(String format)  
		{  
			this.format=format;  
		}  
		public boolean accept(File dir, String name)   
		{  
			//   
			return name.toLowerCase().endsWith(format);  
		}  
	}  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早起的年轻人

创作源于分享

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值