/*cl_tl*/
{
私殼殼;
私人展示展示;
私有文本txtSourceFile
私有文本txtNewFilePath
私有文本txtFileSize
私有按鈕btnChooseFile
私有按鈕btnChoosePath
私有按鈕btnCut
私有按鈕btnUnionFiles
私有按鈕btnUp
私人按鈕btnDown
私有按鈕btnClear
私有按鈕btnClearAll
私有按鈕btnUnion
私有表tblFileList
私有文件sourceFile = null/*源文件*/
私有文件[]new File = null;/*分割後生成的文件*/
private int file count = 0;/*分成*/的文件數
private int fileSize = 0;/*分區文件塊大小*/
私有字符串strSourceFile = null/*源文件路徑和名稱*/
私有字符串strNewFilePath = null/*分割文件存儲路徑*/
公共靜態void main(String[] args)
{
顯示Display = new Display();
file cut Item = new file cut();
item . create shell();
而(!Item.shell.isDisposed())
{
如果(!display.readAndDispatch())
display . sleep();
}
dispose();
}
/*fn_hd
*rem:創建表單
*aut:
*日誌:2005-11-24
*/
私有void createShell()
/*fn_tl*/
{
殼牌=新殼牌(顯示器,SWT。MIN);
shell.setBounds(300,250,500,330);
Shell.setText("文件拆分和合並");
GridLayout shell layout = new GridLayout();
shell layout . num columns = 3;
shell . set layout(shell layout);
create widgets();
shell . open();
}
/**fn_hd
*rem:向窗體添加控件。
*per:
*aut:
*日誌:2005-11-24
*/
私有void createWidgets()
/*fn_tl*/
{
最終標簽lblNull0 =新標簽(殼牌,SWT。無);
GridData gd0 = new GridData();
gd0 . horizontal span = 3;
LBL null 0 . setlayoutdata(gd0);
最終標簽lblSourceFile =新標簽(殼牌,SWT。無);
LblSourceFile.setText("源文檔");
GridData gd2 =新的GridData(GridData。填充_水平);
txtSourceFile =新文本(shell,SWT。邊框);
txtsourcefile . setlayoutdata(gd2);
btnChooseFile =新建按鈕(shell,SWT。推);
btnChooseFile.setText(" .. ");
最終標簽lblPath =新標簽(殼牌,SWT。無);
LblPath.setText("存儲路徑");
txtNewFilePath =新文本(shell,SWT。邊框);
GridData gd3 =新的GridData(GridData。填充_水平);
txtnewfilepath . setlayoutdata(gd3);
btnChoosePath =新建按鈕(shell,SWT。推);
btnChoosePath.setText(" .. ");
最終標簽lblSize =新標簽(殼牌,SWT。無);
LblSize.setText("塊大小(KB)");
txtFileSize =新文本(shell,SWT。邊框);
GridData gd7 =新的GridData(GridData。HORIZONTAL _ ALIGN _ BEGINNING);
txtfilesize . setlayoutdata(gd7);
btnCut =新按鈕(殼牌,SWT。推);
GridData gd4 =新的GridData(GridData。HORIZONTAL _ ALIGN _ BEGINNING);
BTN cut . setlayoutdata(gd4);
BtnCut.setText("開始分段");
最終標簽lbl1 =新標簽(殼牌,SWT。無);
GridData gd8 = new GridData();
gd8 . horizontal span = 3;
LBL 1 . setlayoutdata(gd8);
Lbl1.setText("要合並的文件列表");
tblFileList =新表(shell,SWT。邊框);
GridData gd1 =新的GridData(GridData。FILL _ BOTH);
GD 1 . horizontal span = 2;
tblfilelist . setlayoutdata(GD 1);
Composite com = new Composite(殼牌,SWT。無);
GridLayout com layout = new GridLayout();
com . set layout(com layout);
最終標簽lblNote =新標簽(殼牌,SWT。無);
grid data data = new grid data();
data . horizontal span = 3;
lblNote.setLayoutData(數據);
LblNote.setText("提示:註意合並文件的順序");
btnUnionFiles =新建按鈕(com,SWT。推);
BtnUnionFiles.setText("選擇文件");
btnUp =新按鈕(com,SWT。推);
Bt NUP . settext(" move up ");
Bt NUP . set enabled(false);
btnDown =新按鈕(com,SWT。推);
BtnDown.setText("下移");
BTN down . set enabled(false);
btnClear =新建按鈕(com,SWT。推);
BTN clear . settext(" delete ");
BTN clear . set enabled(false);
btnClearAll =新建按鈕(com,SWT。推);
BtnClearAll.setText("空列表");
btnUnion =新按鈕(com,SWT。推);
BtnUnion.setText("開始合並");
btnCut.addSelectionListener(新選擇適配器()
//添加“開始拆分”監視器
{
public void widgetSelected(selection event事件)
{
cutButtonEvent();
}
});
btnchoosefile . addselectionlistener(新選擇適配器()
//添加選擇(源文件)監視器
{
public void widgetSelected(selection event事件)
{
file dialog FD open = new file dialog(shell,SWT。打開);
string strFileName = FD open . open();
if (strFileName!=空)
{
txtsourcefile . settext(strFileName);
txtnewfilepath . settext(FD open . getfilterpath());
txtfilesize . setfocus();
}
}
});
btnchoosepath . addselectionlistener(新選擇適配器()
//添加選擇(拆分文件存儲路徑)監視器
{
public void widgetSelected(selection event事件)
{
DirectoryDialog dirDia =新directory dialog(shell);
string strfile dir = dirdia . open();
if (strFileDir!=空)
{
txtnewfilepath . settext(strFileDir);
txtfilesize . setfocus();
}
}
});
btnUp.addSelectionListener(新選擇適配器()
//添加“上移”監視器
{
public void widgetSelected(selection event事件)
{
//int[]item indexes = tblfilelist . getselectionindices();
int item index = tblfilelist . getselectionindex();
if (itemIndex == 0)
{
tblfilelist . setfocus();
返回;
}
//交換列表中兩行的內容
string strTemp = tblfilelist . getitem(item index)。getText();
tblFileList.getItem(itemIndex)。setText(tblFileList.getItem(
itemIndex - 1)。getText());
tblfilelist . getitem(item index-1)。setText(strTemp);
//設置焦點
tblfilelist . set selection(item index-1);
tblfilelist . setfocus();
}
});
btnDown.addSelectionListener(新選擇適配器()
//添加“下移”監視器
{
public void widgetSelected(selection event事件)
{
//int[]item indexes = tblfilelist . getselectionindices();
int item index = tblfilelist . getselectionindex();
if(item index = = tblfilelist . getitemcount()-1)
{
tblfilelist . setfocus();
返回;
}
//交換列表中兩行的內容
string strTemp = tblfilelist . getitem(item index)。getText();
tblFileList.getItem(itemIndex)。setText(tblFileList.getItem(
itemIndex + 1)。getText());
tblfilelist . getitem(item index+1)。setText(strTemp);
//設置焦點
tblfilelist . set selection(item index+1);
tblfilelist . setfocus();
}
});
btnClear.addSelectionListener(新的SelectionAdapter()
//添加“刪除”監視器
{
public void widgetSelected(selection event事件)
{
int item index = tblfilelist . getselectionindex();
tblfilelist . remove(item index);
Bt NUP . set enabled(false);
BTN down . set enabled(false);
BTN clear . set enabled(false);
}
});
btnclearal . addselectionlistener(新的SelectionAdapter()
//添加“空列表”監視器
{
public void widgetSelected(selection event事件)
{
tblfilelist . remove all();
Bt NUP . set enabled(false);
BTN down . set enabled(false);
BTN clear . set enabled(false);
}
});
txtfilesize . addselectionlistener(新選擇適配器()
//添加“塊大小”文本框,輸入Enter Monitor。
{
public void widgetDefaultSelected(selection event事件)
{
cutButtonEvent();
}
});
btnunionfiles . addselectionlistener(新選擇適配器()
//添加“選擇文件”監視器
{
public void widgetSelected(selection event事件)
{
file dialog FD = new file dialog(shell,SWT。多);
fd.setFilterExtensions(新字符串[]{"*。dat "," *。*"});
if (fd.open()!=空)
{
string[]strFiles = FD . get filenames();
string strfile path = FD . getfilterpath();
//strUnionFilePath = new String[strfiles . length];
for(int I = 0;我& ltstrFiles.lengthi++)
{
//strUnionFilePath[I]= FD . getfilterpath();
table item item = new table item(tblFileList,SWT。無);
item . settext(strfile path+" \ \ "+strFiles[I]);
}
}
}
});
btnUnion.addSelectionListener(新選擇適配器()
//添加“開始合並”監視器
{
public void widgetSelected(selection event事件)
{
if(tblfilelist . getitemcount()= = 0)
{
返回;
}
開關(unionFiles())
{
案例1:
ShowMessage("成功","合並完成!",
SWT。好的| SWT。圖標_信息);
tblfilelist . remove all();
Bt NUP . set enabled(false);
BTN down . set enabled(false);
BTN clear . set enabled(false);
打破;
案例-1:
ShowMessage ("Error ","文件不存在!",
SWT。好的| SWT。ICON _ ERROR);
打破;
案例二:
打破;
默認值:
ShowMessage ("Error ","出現錯誤,文件合並失敗!",
SWT。好的| SWT。ICON _ ERROR);
}
}
});
tblfilelist . addselectionlistener(新選擇適配器()
//在選定列表中添加元素監視器
{
public void widgetSelected(selection event事件)
{
Bt NUP . set enabled(true);
BTN down . set enabled(true);
BTN clear . set enabled(true);
}
});
}
/**fn_hd
*rem:顯示消息框
*日誌:2005-11-24
*/
private int show message(String strText,String strMessage,int i)
{/*fn_tl*/
MessageBox msgBox = new MessageBox(shell,I);
msgbox . settext(strText);
msgbox . set message(strMessage);
返回msgbox . open();
}
/**fn_hd
*rem:點擊“拆分”按鈕,觸發事件響應。
*日誌:2005-11-24
*/
私有void cutButtonEvent()
/*fn_tl*/
{
strSourceFile = txtsourcefile . gettext()。trim();
strNewFilePath = txtnewfilepath . gettext()。trim();
if(strsourcefile . equals(" " | | strnewfilepath . equals(" ")
{
ShowMessage ("prompt ","請輸入源文件和\n\n拆分文件的路徑!",
SWT。好的| SWT。圖標_信息);
返回;
}
嘗試
{
fileSize = integer . parse int(txt fileSize . gettext());
fileSize * = 1024;
if(文件大小& lt= 0)
{
ShowMessage ("Error ","塊大小是正整數!",
SWT。好的| SWT。ICON _ ERROR);
返回;
}
}
捕捉(例外e)
{
ShowMessage ("Error ","請在塊大小框中填入數字!",
SWT。好的| SWT。ICON _ ERROR);
返回;
}
開關(cutFile())
{
案例1:
ShowMessage ("prompt ","拆分完成!",SWT。好的|
SWT。圖標_信息);
txtsourcefile . settext(" ");
txtnewfilepath . settext(" ");
txtfilesize . settext(" ");
打破;
案例-1:
ShowMessage ("Error ","源文件不存在或存儲路徑不存在!",
SWT。好的| SWT。ICON _ ERROR);
打破;
默認值:
ShowMessage("未知錯誤","文件分區失敗!",
SWT。好的| SWT。ICON _ ERROR);
}
}
/*fn_hd
*rem:文件分段實現
*per: 1成功返回,如果找不到文件則返回-1,其他情況下返回0。
*exp:IOException
*aut:
*日誌:2005-11-22,已創建
*日誌:2005-11-24,已修改。
*/
私有int cutFile()
/*fn_tl*/
{
sourceFile =新文件(strSourceFile);
file count =(int)(source file . length()/fileSize);
if(source file . length()% fileSize!= 0)
{
filecount++;
}
newFile =新文件[文件計數];
嘗試
{
int count = 0;
int I = 0;
byte[] bueff =新字節[文件大小];
FileOutputStream out = null
file inputstream in = new file inputstream(source file);
for(I = 0;我& ltnewFile.lengthi++)
{
newFile[i] =新文件(strNewFilePath,
i + sourceFile.getName() +"。dat”);
}
I = 0;
while ((count = in.read(bueff,0,fileSize))!= -1)
{
out = new file output stream(new file[I]);
out.write(bueff,0,count);
out . close();
i++;
}
in . close();
返回1;
}
catch(FileNotFoundException e)
{
system . out . println(e);
return-1;
}
捕捉(IOException e)
{
system . out . println(e);
返回0;
}
}
/*fn_hd
*rem:文件合並的實現
*per: 1成功返回,找不到文件返回-1,取消操作返回-2,其他情況返回0;
*aut:
*exp:FileNotFoundException,IOException
*日誌:2005-11-28,已創建
*/
私有int unionFiles()
/*fn_tl*/
{
String[]strFiles = new String[tblfilelist . getitemcount()];
File[] unionFiles =新文件[strfiles . length];
file dialog FD save = new file dialog(shell,SWT。保存);
string s = FD save . open();
if (s == null)
{
return-2;
}
File out File = new File(FD save . getfilterpath(),
FD save . get filename());
if (outFile.exists())
{
Int msg = showMessage ("prompt ","該文件存在,是否替換?",
SWT。是的| SWT。不| SWT。ICON _ QUESTION);
if (msg == SWT。否)
{
return-2;
}
}
for(int I = 0;我& ltstrFiles.lengthi++)
{
strFiles[I]= tblfilelist . getitem(I)。getText();
}
嘗試
{
FileInputStream in = null
file output stream out = new file output stream(outFile);
byte[] buff =新字節[1024];
int計數;
for(int I = 0;我& ltstrFiles.lengthi++)
{
in = new file inputstream(strFiles[I]);
while((count = in.read(buff,0,1024))!= -1)
{
out.write(buff,0,count);
}
in . close();
}
out . close();
返回1;
}
catch(FileNotFoundException e)
{
system . out . println(e);
return-1;
}
捕捉(IOException e)
{
system . out . println(e);
返回0;
}
}