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

📄 to merge and break up the document.java

📁 to merge and break up the document
💻 JAVA
字号:
/*
 *file name:FileCut.java
 *
 *2005-11-22
 *
 */

//package myproject.fileCut; 
 
import java.io.*; 
import org.eclipse.swt.events.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import java.text.*;


 /**cl_hd
  *rem:将文件分割成指定大小的文件块
  *aut:刘畅
  *log:2005-11-22,创建
  */
public class FileCut
/*cl_tl*/
{
	private Shell shell;
	private Display display;
	private Text txtSourceFile;
	private Text txtNewFilePath;
	private Text txtFileSize;
	private Button btnChooseFile;
	private Button btnChoosePath;
	private Button btnCut;
	private Button btnUnionFiles;
	private Button btnUp;
	private Button btnDown;
	private Button btnClear;
	private Button btnClearAll;
	private Button btnUnion;
	private Table tblFileList;

	private File sourceFile = null;				/*源文件*/
	private File[] newFile = null;				/*分割后产生的文件*/	
	private int fileCount = 0;					/*分割成的文件个数*/
	private int fileSize = 0;					/*分割的文件块大小*/
	private String strSourceFile = null;		/*源文件路径及名称*/
	private String strNewFilePath = null;		/*分割文件存放路径*/
	
	public static void main(String[] args)
	{
		Display	display=new Display();
		FileCut Item=new FileCut();
		Item.createShell();
	
		while( !Item.shell.isDisposed())
	    { 
	        if(!display.readAndDispatch())
	            display.sleep();
	    }
	    display.dispose();		
	}
	
	
	/*fn_hd
	 *rem:创建窗体
	 *aut:
	 *log:2005-11-24
	 */
	private void createShell()
	/*fn_tl*/
	{
		shell = new Shell(display, SWT.MIN);
		shell.setBounds(300,250,500,330);
		shell.setText("文件分割合并");
		GridLayout shellLayout = new GridLayout(); 
		shellLayout.numColumns = 3;
		shell.setLayout(shellLayout);
		createWidgets();
		shell.open();
	}	
	
	/**fn_hd
	 *rem:在窗体内添加控件
	 *per:
	 *aut:
	 *log:2005-11-24
	 */
	private void createWidgets()
	/*fn_tl*/
	{
		final Label lblNull0 = new Label(shell,SWT.None);
		GridData gd0 = new GridData();
		gd0.horizontalSpan = 3;
		lblNull0.setLayoutData(gd0);
		
		final Label lblSourceFile = new Label(shell, SWT.None);
		lblSourceFile.setText("源 文 件");
		
		GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
		txtSourceFile = new Text(shell, SWT.BORDER);
		txtSourceFile.setLayoutData(gd2);
		
		btnChooseFile = new Button(shell, SWT.PUSH);
		btnChooseFile.setText("..");
		
		final Label lblPath = new Label(shell, SWT.None);
		lblPath.setText("存放路径");
		
		txtNewFilePath = new Text(shell, SWT.BORDER);
		GridData gd3 = new GridData(GridData.FILL_HORIZONTAL);
		txtNewFilePath.setLayoutData(gd3);
		
		btnChoosePath = new Button(shell, SWT.PUSH);
		btnChoosePath.setText("..");
		
		final Label lblSize = new Label(shell, SWT.None);
		lblSize.setText("分块大小(KB)");
		
		txtFileSize = new Text(shell,SWT.BORDER);
		GridData gd7 = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		txtFileSize.setLayoutData(gd7);
		
		btnCut = new Button(shell, SWT.PUSH);
		GridData gd4 = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		btnCut.setLayoutData(gd4);
		btnCut.setText("开始分割");
		
		final Label lbl1 = new Label(shell, SWT.None);
		GridData gd8 = new GridData();
		gd8.horizontalSpan = 3;
		lbl1.setLayoutData(gd8);
		lbl1.setText("待合并的文件列表");
		
		tblFileList = new Table(shell, SWT.BORDER);
		GridData gd1 = new GridData(GridData.FILL_BOTH);
		gd1.horizontalSpan = 2;
		tblFileList.setLayoutData(gd1);
		
		Composite com = new Composite(shell, SWT.None);
		GridLayout comLayout = new GridLayout();
		com.setLayout(comLayout);
		
		final Label lblNote = new Label(shell, SWT.None);
		GridData data = new GridData();
		data.horizontalSpan=3;
		lblNote.setLayoutData(data);
		lblNote.setText("提示:注意合并文件的顺序");
		
		btnUnionFiles = new Button(com, SWT.PUSH);
		btnUnionFiles.setText("选择文件");
		
		btnUp = new Button(com, SWT.PUSH);
		btnUp.setText("  上移  ");
		btnUp.setEnabled(false);
	
		btnDown = new Button(com, SWT.PUSH);
		btnDown.setText("  下移  ");
		btnDown.setEnabled(false);
			
		btnClear = new Button(com, SWT.PUSH);
		btnClear.setText("  删除  ");
		btnClear.setEnabled(false);
		
		btnClearAll = new Button(com, SWT.PUSH);
		btnClearAll.setText("清空列表");
		
		btnUnion = new Button(com, SWT.PUSH);
		btnUnion.setText("开始合并");
		
		btnCut.addSelectionListener(new SelectionAdapter()
		//添加"开始分割"监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				cutButtonEvent();
			}
		});
		
		btnChooseFile.addSelectionListener(new SelectionAdapter()
		//添加选择(源文件)监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				FileDialog fdOpen = new FileDialog(shell,SWT.OPEN);
				String strFileName = fdOpen.open();
				if (strFileName != null)
				{
					txtSourceFile.setText(strFileName);
					txtNewFilePath.setText(fdOpen.getFilterPath());
					txtFileSize.setFocus();
				}
			}
		});		
		
		btnChoosePath.addSelectionListener(new SelectionAdapter()
		//添加选择(分割文件存放路径)监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				DirectoryDialog dirDia = new DirectoryDialog(shell);
				String strFileDir = dirDia.open();
				if (strFileDir != null)
				{
					txtNewFilePath.setText(strFileDir); 
					txtFileSize.setFocus();					
				}
			}
		});
		
		btnUp.addSelectionListener(new SelectionAdapter()
		//添加"上移"监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				//int[] itemIndices = tblFileList.getSelectionIndices();
				int itemIndex = tblFileList.getSelectionIndex();
				if (itemIndex == 0)
				{
					tblFileList.setFocus();
					return;
				}
				//交换列表中两行的内容
				String strTemp = tblFileList.getItem(itemIndex).getText();
				tblFileList.getItem(itemIndex).setText(tblFileList.getItem(
												itemIndex - 1).getText());
				tblFileList.getItem(itemIndex - 1).setText(strTemp);
				//设置焦点
				tblFileList.setSelection(itemIndex - 1);
				tblFileList.setFocus();								
			}
		});	
		
		btnDown.addSelectionListener(new SelectionAdapter()
		//添加"下移"监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				//int[] itemIndices = tblFileList.getSelectionIndices();
				int itemIndex = tblFileList.getSelectionIndex();
				if (itemIndex == tblFileList.getItemCount() - 1)
				{
					tblFileList.setFocus();
					return;
				}
				//交换列表中两行的内容
				String strTemp = tblFileList.getItem(itemIndex).getText();
				tblFileList.getItem(itemIndex).setText(tblFileList.getItem(
												itemIndex + 1).getText());
				tblFileList.getItem(itemIndex + 1).setText(strTemp);
				//设置焦点
				tblFileList.setSelection(itemIndex + 1);
				tblFileList.setFocus();								
			}
		});				
		
		btnClear.addSelectionListener(new SelectionAdapter()
		//添加"删除"监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				int itemIndex = tblFileList.getSelectionIndex();
				tblFileList.remove(itemIndex);
				btnUp.setEnabled(false);
				btnDown.setEnabled(false);				
				btnClear.setEnabled(false);
			}
		});		
		
		btnClearAll.addSelectionListener(new SelectionAdapter()
		//添加"清空列表"监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				tblFileList.removeAll();
				btnUp.setEnabled(false);
				btnDown.setEnabled(false);			
				btnClear.setEnabled(false);
			}
		});
		
		txtFileSize.addSelectionListener(new SelectionAdapter()
		//添加"分块大小"文本框中输入回车监视器
		{
			public void widgetDefaultSelected(SelectionEvent event)
			{
				cutButtonEvent();
			}
		});
		
		btnUnionFiles.addSelectionListener(new SelectionAdapter()
		//添加"选择文件"监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				FileDialog fd = new FileDialog(shell, SWT.MULTI);
				fd.setFilterExtensions(new String[]{"*.dat", "*.*"});				
				if (fd.open() != null)
				{
					String[] strFiles = fd.getFileNames();
					String strFilePath = fd.getFilterPath();
					//strUnionFilePath = new String[strFiles.length];	
					for(int i = 0; i < strFiles.length; i++)			
					{
						//strUnionFilePath[i] = fd.getFilterPath();						
						TableItem item = new TableItem(tblFileList, SWT.None);
						item.setText(strFilePath + "\\" + strFiles[i]);
					}
				}
			}
		});
		
		btnUnion.addSelectionListener(new SelectionAdapter()
		//添加"开始合并"监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				if (tblFileList.getItemCount() == 0)
				{
					return;
				}
				switch (unionFiles())
				{
					case 1:
						showMessage("成功", "合并完成!", 
									SWT.OK | SWT.ICON_INFORMATION);
						tblFileList.removeAll();
						btnUp.setEnabled(false);
						btnDown.setEnabled(false);			
						btnClear.setEnabled(false);						
						break;
					case -1:
						showMessage("错误", "文件不存在!", 
									SWT.OK | SWT.ICON_ERROR);
						break;
					case -2:
						break;	
					default:
						showMessage("错误", "有错误发生,文件合并失败!",
									SWT.OK | SWT.ICON_ERROR);				
				}
			}
		});
		
		tblFileList.addSelectionListener(new SelectionAdapter()
		//添加选中列表中元素监视器
		{
			public void widgetSelected(SelectionEvent event)
			{
				btnUp.setEnabled(true);
				btnDown.setEnabled(true);
				btnClear.setEnabled(true);
			}
		});
	}
	
	
	/**fn_hd
	 *rem:显示消息框
	 *log:2005-11-24
	 */
	private int showMessage(String strText, String strMessage, int i)
	{/*fn_tl*/
		MessageBox msgBox = new MessageBox(shell,i);
		msgBox.setText(strText);
		msgBox.setMessage(strMessage);
		return msgBox.open();
	}
	
	/**fn_hd
	 *rem:点击"分割"按钮触发的事件响应
	 *log:2005-11-24
	 */
	private void cutButtonEvent()
	/*fn_tl*/
	{
		strSourceFile = txtSourceFile.getText().trim();
		strNewFilePath = txtNewFilePath.getText().trim();

		if (strSourceFile.equals("") || strNewFilePath.equals(""))
		{
			showMessage("提示", "请输入源文件和		\n\n分割文件的路径!  ",
						 SWT.OK | SWT.ICON_INFORMATION);
			return;
		}
		try
		{
			fileSize = Integer.parseInt(txtFileSize.getText());
			fileSize *= 1024;
			if (fileSize <= 0)
			{
				showMessage("错误", "分块大小为正整数!	", 
							SWT.OK | SWT.ICON_ERROR);
				return;			
			}	
		} 
		catch(Exception e)
		{
			showMessage("错误", "请在分块大小框填入数字!		", 
						SWT.OK | SWT.ICON_ERROR);	
			return;
		}				
		switch (cutFile())
		{
			case 1:
				showMessage("提示", "分割完成!	", SWT.OK |
							 SWT.ICON_INFORMATION);
				txtSourceFile.setText("");
				txtNewFilePath.setText("");
				txtFileSize.setText("");			 
				break;
			case -1:
				showMessage("错误", "源文件不存在或存放路径不存在!",
							SWT.OK | SWT.ICON_ERROR);
				break;
			default:
				showMessage("未知错误", "文件分割失败!	", 
							SWT.OK | SWT.ICON_ERROR);				
		}
	}
	
	/*fn_hd
	 *rem:文件分割实现
	 *per:成功返回1,文件未找到返回-1,其他情况返回0
	 *exp:IOException
	 *aut:
	 *log:2005-11-22,创建
	 *log:2005-11-24,修改
	 */
	private int cutFile()
	/*fn_tl*/
	{
		sourceFile = new File(strSourceFile);
		fileCount = (int) (sourceFile.length() / fileSize);
		if (sourceFile.length() % fileSize != 0)
		{
			fileCount++;
		}
		newFile = new File[fileCount];

		try
		{
			int count = 0;
			int i = 0;
			byte[] bueff = new byte[fileSize];
			FileOutputStream out = null;
			FileInputStream in = new FileInputStream(sourceFile);
			for (i = 0; i < newFile.length; i++)
			{
				newFile[i] = new File(strNewFilePath,
	   				  	         i + sourceFile.getName() + ".dat");
			}
			i = 0;
			while ((count = in.read(bueff,0,fileSize)) != -1)
			{
					out = new FileOutputStream(newFile[i]);
					out.write(bueff,0,count);
					out.close();
					i++;	
			}
			in.close();
			return 1;
		}
		catch(FileNotFoundException e)
		{
			System.out.println(e);	
			return -1;
		}
		catch(IOException e)
		{
			System.out.println(e);
			return 0;
		}
	}
	
	/*fn_hd
	 *rem:文件合并的实现
	 *per:成功返回1,文件未找到返回-1,取消操作返回-2,其他情况返回0;
	 *aut:
	 *exp:FileNotFoundException,IOException
	 *log:2005-11-28,创建
	 */
	private int unionFiles()
	/*fn_tl*/
	{
		String[] strFiles = new String[tblFileList.getItemCount()];
		File[] unionFiles = new File[strFiles.length];  	
		FileDialog fdSave = new FileDialog(shell, SWT.SAVE);
		String s = fdSave.open();
		if (s == null)
		{
			return -2;
		}
		File outFile = new File(fdSave.getFilterPath(), 
								fdSave.getFileName());
		if (outFile.exists())
		{
			int msg = showMessage("提示", "该文件以存在,是否替换?",
								  SWT.YES | SWT.NO | SWT.ICON_QUESTION);
			if (msg == SWT.NO)
			{
				return -2;
			}					  
		}						
		for(int i = 0; i < strFiles.length; i++)
		{
			strFiles[i] = tblFileList.getItem(i).getText();
		}
		try
		{
			FileInputStream in = null;
			FileOutputStream out = new FileOutputStream(outFile);
			byte[] buff = new byte[1024];
			int count;
			for(int i = 0; i < strFiles.length; i++)
			{
				in = new FileInputStream(strFiles[i]);
				while((count = in.read(buff,0,1024)) != -1)
				{
					out.write(buff,0,count);
				}
				in.close();
			}
			out.close();
			return 1;
		}
		catch(FileNotFoundException e)
		{
			System.out.println(e);
			return -1;
		}
		catch(IOException e)
		{
			System.out.println(e);
			return 0;
		}	
	}
	
}

⌨️ 快捷键说明

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