?? cutandunit.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 + -