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

📄 cutandunit.java

📁 实现了文件的切割和合并 很详细
💻 JAVA
字号:
import java.io.*;
import java.util.*;

public class CutAndUnit {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		
		jieshao();
		short mainFunc = (new Scanner(System.in)).nextShort();
		
		if( mainFunc == 1 )
		{
		FileCut fileCut = new FileCut();
		fileCut.Cut();
		}
		else if( mainFunc == 2 )
		{
			FileUnite fileUnit = new FileUnite();
			fileUnit.Unite();
		}
		
	}
	static void jieshao()
	{
		System.out.println("功能介绍:" );
		System.out.println( "1.输入 1 切割文件" );
		System.out.println( "2.输入 2 合并文件" );
		System.out.print( "请输入您的选择:" );
	}
}
/*
 * file.getName:获得文件名
 * file.getPath: 获得文件的路径名
 * 
 * 
 */
class FileCut
{
	String fileSrc;
	String fileTag;
	int fileSize;
	int fileNum;
	byte []typeBuffer = new byte[1024];
	
	FileCut(){ fileSrc = null; fileTag = null; fileNum = 0; fileSize = -1; }
	FileCut( String fileSrc, String fileTag, int fileSize ){ this.fileSrc = new String(fileSrc); this.fileTag = new String( fileTag);fileNum = 0; this.fileSize = fileSize; }
	/*
	 * 1.whether FileSrc and FileTag are initialiazed
	 * 2.func:get each file's size
	 * 3.func:create fileINputStream 
	 * 4.func:create fileOutputStream
	 */
	void Cut()
	{
		if( fileSrc == null )
		{
			System.out.print("Please input the name of the sourceFile:" );
			fileSrc = new String();
			fileSrc = ( new Scanner( System.in ) ).nextLine();
		}
		
		getSize();                                  //获得每个文件的大小
		
		File file = new File( fileSrc );                         //在源文件所在的目录创建一个文件夹,名为:切割
		File fileDir = new File( file.getParent() + "\\切割" + file.getName().substring( 0, file.getName().indexOf('.')) );
		fileDir.mkdir( );                                         //////////创建文件夹
		
		File fileOut = new File( getFileOutName( fileDir, file ) );
		
		//System.err.println( fileOut.getPath() );
		
		byte buffer[] = new byte[1024];                 //建立缓冲
		FileInputStream fileInput = null;
		FileOutputStream fileOutput = null;
		try{
			fileInput = new FileInputStream( fileSrc );
			fileOutput = new FileOutputStream( fileOut.getPath() );     ////////////////如果文件不存在会自动创建
		
		
			int tempint = 0;
			int readCount = -1;                               //该变量通过和fileSize比较确定现在文件大小
			while( (tempint = fileInput.read( buffer, 0, 1024 )) != -1 )    //从源文件读数据,然后输出到目标文件中去
			{
				readCount ++;
				if( readCount < fileSize )
				{
					fileOutput.write( buffer, 0, tempint );
				}
				else
				{
					readCount = 0;
					fileOutput.close();
					fileOutput = new FileOutputStream( getFileOutName(fileDir, file) );
					fileOutput.write( buffer,0, tempint );
				}
			}
		}catch( IOException e ){ System.err.println("Error1" );}
		finally
		{
			try{
			if( fileOutput != null ){ fileOutput.close();}
			if( fileInput != null ){ fileInput.close(); }
			}catch( IOException e ){ System.err.println( "Error4" );}
		}
	}
/*
 * 获得当前目标文件的路径
 */
	String getFileOutName(File fileDir, File file )
	{
		fileNum ++;
		int index = file.getName().indexOf('.');                         //获得‘。’的位置
		String temp = new String( file.getName().substring( index ) );   //获得‘。’及其后面的字符串
		return fileDir.getPath() + "\\" + file.getName().replace(temp, "_" + fileNum + ".vsm");
	}
/*
 * 获得目标文件的大小
 */
	boolean getSize()
	{
		if( fileSize != -1 ) return true;       //判断是否已经被初始化
		
		String temp = new String();
		System.out.print("Please input each cutFile's size:" );
		temp = (new Scanner( System.in ) ).nextLine();
		String regex = "\\d{0,}[kKmMgG]";          
		char sizeUnit;
		
		if( temp.matches( regex ) )                //正则匹配
		{
			int tempLen = temp.length();
			sizeUnit = temp.charAt(tempLen - 1);
			StringBuffer tempStr = new StringBuffer( temp );
			tempStr.deleteCharAt( tempLen - 1 );
			fileSize = Integer.parseInt( tempStr.toString() );
		}
		else
		{
			System.out.println( "输入格式错误,请正确输入,例如:1024M" );
			getSize();
			return false;
		}
		
		if( sizeUnit == 'm' || sizeUnit == 'M' )            //根据单位得到fileSize的大小
			fileSize = fileSize << 10;                      ///////////一位后应将值重新赋给目标变量
		else if( sizeUnit == 'g' || sizeUnit == 'G' )
			fileSize = fileSize << 20;
		
		return true;
	}
}
/*
 * 合并有FileCut分割成的文件
 */
class FileUnite
{
	String fileTagName;
	int fileNum;
	String fileParentName;
	String[] filesName;            //所有被合并的文件的路径名
	File [] files;
	
	FileUnite(){ fileTagName = null; fileNum = 1; fileParentName = null;}
	FileUnite(String fileTag, int fileNum, String fileParent){ this.fileTagName = new String(fileTag); this.fileNum = fileNum; this.fileParentName =new String(fileParent ); }
	FileUnite(String fileTag, int fileNum, String[] filesName ){ this.fileTagName = new String(fileTag); this.fileNum = fileNum; this.filesName = filesName; }
	
	void Unite()
	{
		if( fileTagName == null )
		{
			System.out.print( "请输入合并成文件的路径名:");
			fileTagName = new String( (new Scanner(System.in)).nextLine() );
			System.out.print("请输入包含被合并文件的文件夹:");
			fileParentName = new String( (new Scanner(System.in)).nextLine() );
		}
		
		//getAllFile();
		getAllFile();
		
		UniteAndOutput();
	}
	void getAllFile()
	{
		if( files != null )
		{
			
		}
		File fileParent = new File( fileParentName );
		files = fileParent.listFiles();
		for( int i = 0; i < files.length ; i ++ )
		{
			if( files[i].getName().endsWith(".vsm") )
			{
				int index = files[i].getName().indexOf('_');
				index = files[i].getName().charAt( index + 1 ) - '0';
				files[index] = files[i];
				if( index > fileNum ) 
					fileNum = index;
			}
		}
	}
	void UniteAndOutput()
	{
		FileInputStream fileInput = null;
		FileOutputStream fileOutput = null;
		try
		{
			fileOutput = new FileOutputStream( fileTagName );
			byte buffer[] = new byte[1025];
			
			for( int i = 1; i <= fileNum; i ++ )
			{
				int bufferLen = 0;
				fileInput = new FileInputStream( files[i] );
				while( (bufferLen = fileInput.read( buffer, 0, 1024 ) ) != -1 )
				{
					fileOutput.write( buffer, 0, bufferLen );
				}
			}
		}catch( IOException e ){ System.err.println( "Error2" ); }
		finally
		{
			try{
				if( fileInput != null ){ fileInput.close(); }
				if( fileOutput != null ){ fileOutput.close(); }
			}catch( IOException e ){ System.err.println( "Error5" ); }
		}
	}
/*
 * 以下为合并任意文件的代码, 但是没有在main方法中实现,只是测试了一下,而且可以用
 */
	void SetFileUnite(String fileTag, int fileNum, String[] filesName ){ this.fileTagName = new String(fileTag); this.fileNum = fileNum; this.filesName = filesName; }
	void UniteAnyFile( )
	{
		System.out.println( "请在下方输入所有将被合并的文件的路径名(每行一个文件路径名,单独一行输入 -1 表示输入结束):");
		Scanner scanner = new Scanner( System.in );
		fileNum = 1;
		filesName = new String[1001];
		files = new File[1000];
		
		try{
			while( true )
			{	
				if( scanner.hasNextLine() && !scanner.hasNextInt() )
				{
					filesName[fileNum] = scanner.nextLine();
					files[fileNum] = new File( filesName[fileNum++]);
				}
				else if( scanner.hasNextInt() )
					break;
			}
		}catch( Exception e ){ System.err.println( "Error3" ); }
		fileNum --;
	}
}
	
	
	
	
	
	
	
	
	
	
	
	
	

⌨️ 快捷键说明

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