⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesplitter.java

📁 char22-2 文件拆分实例 提供了本书第2章的实例文件拆分实例的源程序;
💻 JAVA
字号:
import java.io.*;

//构建FileSplitter类,进行文件拆分
class FileSplitter
{
	void Split(String f_name) throws Exception
	{
		fin = new FileInputStream(f_name);
		//通过FileInputStream类来封装文件对象
		fout1= new FileOutputStream("merge.bat");
		String str = "copy /b ";
		byte buff1[] = str.getBytes();
		
		for (int j=0; j < buff1.length; j += 1)
			fout1.write(buff1[j]);
		
		while (fin.available()!=0 && fin.available()>=1048576)
		{
			//判断是否满足条件
			if (!FLAG)
			{
				fout1.write((char) file_no);
				fout1.write((char) temp);
				fout1.write((char) 43);
			}
			
			if (count<=9)
			{
				
				arr[0]=(char) file_no;
				arr[1] = (char) temp;
				temp++;
				count++;
				name = new String(arr);
				
				if (count>9)
				{
					count=0;
					file_no+=1;
					temp = 48;
				}
			}
			
			fout = new FileOutputStream(name);
			
			for(long i=0;i<=1047552;i+=8192)
			{
				
				fin.read(buff);
				fout.write(buff);
			}
			
			fout.close();
		} // while循环终结
		
		if (fin.available()<1048576)
		{
			fout1.write((char) file_no);
			fout1.write((char) temp);
			
			if (count<=9)
			{
				
				arr[0]=(char) file_no;
				arr[1] = (char) temp;
				temp++;
				count++;
				name = new String(arr);
				
				if (count>9)
				{
					count=0;
					file_no+=1;
					temp = 48;
				}
			}
			fout = new FileOutputStream(name);
			
			do
			{
				a= fin.read();
				if(a!=-1) fout.write(a);
			}while(a!=-1);
		}
		
		fout.close();
		
		//构建另一个文件,作为拆分用
		String another_filename;
		int te = f_name.lastIndexOf('\\');
		te+=1;
		another_filename = f_name.substring(te);
		
		str = " \"" + another_filename + "\"\r\n";
		
		byte buff2[] = str.getBytes();
		
		for (int j=0; j < buff2.length; j += 1)
			fout1.write(buff2[j]);
		
		str = "del ??\r\n";
		
		byte buff3[] = str.getBytes();
		
		for (int j=0; j < buff3.length; j += 1)
			fout1.write(buff3[j]);
		
		str = "exit";
		
		byte buff4[] = str.getBytes();
		
		for (int j=0; j < buff4.length; j += 1)
			fout1.write(buff4[j]);
		
		fout1.close();
		fin.close(); 
		
		
	}//Split 方法终结
	
	
	byte buff[] = new byte[8192];
	OutputStream fout;
	OutputStream fout1;
	InputStream fin;
	int file_no=48;
	String name;
	int count;
	char arr[] = new char[3];
	int temp = file_no ;
	int a;
	//重新设置标志位
	boolean FLAG =  false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -