文件切割 java

package 文件切割;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

public class 文件切割器 {
	public static void main(String[] args) throws IOException {//不要随便运行。。。
		//delet();
		//spiltfilebynum(new File("d:\\dd.txt"), 3);
		//spiltfilebymemory(new File("d:\\dd.txt"),1024*12);
	}
	//输入文件和文件需要被切割的份数 按份数切
	static void spiltfilebynum(File file,int n) throws IOException
	{
		String s=file.getParent();//获取文件路径,用于确定将来被切割的文件放置的路径
		FileInputStream f=new FileInputStream(file);//确定输入流
		
		int count=1;
		long b=file.length();//获取文件长度
		
		int space =(int)b/n;	//开辟数组
		byte arr[]=new byte[space];
		
		int len;
		while((len=f.read(arr))!=-1)
		{
			FileOutputStream fos=new FileOutputStream(new File(s,(count++)+".hahhh"));//确定输出流
			fos.write(arr, 0, len);
			fos.close();
		}
		f.close();
	}
	//告诉我要切多大内存  按内存切
	static void spiltfilebymemory(File file,int n) throws IOException
	{
		int count=1;
		
		String s=file.getParent();
		FileInputStream f=new FileInputStream(file);
		
		byte arr[]=new byte[n];
		
		int len;
		while((len=f.read(arr))!=-1)
		{
			FileOutputStream f2=new FileOutputStream(new File(s,(count++)+".asds")); 
			f2.write(arr, 0, len);
			f2.close();
		}
		f.close();
	}
	static void delet(String s)//用于搞错了删除文件,创了2000多个文件2333
	{
		FilenameFilter f=new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return name.endsWith(s);
			}
		};
		File file=new File("d:\\");
		File arr[]=file.listFiles();
		for (File file2 : arr) {
			if(file2.isDirectory()) {}
			else
				if(f.accept(file, file2.getName())) 
				{
					file2.delete();
				}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/lioncatch/article/details/84558394
今日推荐