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

📄 filecut.java

📁 一个JAVA实现的文件切割工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.Vector;
import javax.swing.event.*;
public class FileCut extends JPanel implements ActionListener,TableModelListener,Runnable {
	/**
	 * 文件切割机1.3版FileCut extends Jpanel(public)
	 */
	private static final long serialVersionUID = -2910248303777262867L;
	private JLabel message=new JLabel("请先选择源文件及切割后存放的文件夹,接着设置目标存储方式,最后单击\"开始切割\"");
	//-
	TableTableModel myModel=new TableTableModel();
	private  Vector fileLen=myModel.fileLen;//文件的长度,long
	private  Vector subLen=myModel.subLen;//文件及切成每块长度,long
	JTable src = new JTable(myModel);//表格对象table的数据来源是myModel对象
	private static int n=1;	//用来控制改变块大小值的一个控制变量
	private int FWsub=TableTableModel.FWsub;//我的范围块数引用
	private JButton srcChoice =new JButton("添加...");
	private JButton srcDel =new JButton("删除");
	private JButton srcAllDel =new JButton("全部删除");
	private JButton srcSetSub=new JButton("块数设置...");
	private JButton srcAllSetSub =new JButton("统一设置块数...");
	//-
	private JTextField dest=new JTextField(20);
	private JButton destChoice =new JButton("浏览...");	
	private ButtonGroup destSaveMode=new  ButtonGroup();//切割存储模式
	private JRadioButton options[]=new JRadioButton[2];//模式选项
	private String optionLabel[]={"为每个文件创建自己的块存储目录","统一放在目标根目录下"};//模式选项文本
	private JPanel optionPanel=new JPanel();
	//-
	private JButton startCut =new JButton("开始切割");
	private JButton quit =new JButton("退出");
	private JPanel srcPanel=new JPanel();//切割的文件面板
	private JPanel srcCtrPanel=new JPanel();//切割的控制面板
	private JPanel nextPanel=new JPanel();//除文件(控制)面板之外的所有面板集合。为了排版
	private JPanel destPanel=new JPanel();//切割后存放的文件夹面板
	private JPanel msgPanel=new JPanel();//消息面板
	private JPanel ctrPanel=new JPanel();//控件面板
	private String destDirectory;
	
	private JProgressBar subBar,allBar;//进度条
	private JLabel subBarLabel=new JLabel("正在切割的文件")
					,allBarLabel=new JLabel("总进度:");
	private JPanel barPanel=new JPanel();
	
	private String fileString[];//每次从filechooser选择的源文件列表,仅作一临时变量
	private int nFile=2;//文件的块数,默认为2,int
	private JTabbedPane page;//页,用来回调页的方法
	private int selectFilesNumber=0;//选择文件的个数
	
	
	public FileCut(JTabbedPane page){//构造函数		
		myModel.addTableModelListener(this);
		src.setPreferredScrollableViewportSize(new Dimension(this.getWidth(),this.getHeight()/3));
		srcPanel.setLayout(new BorderLayout());
		JScrollPane srcPane = new JScrollPane(src);
		srcPanel.add(srcPane,"Center");
		srcPanel.add(srcCtrPanel,"South");
		srcCtrPanel.add(srcChoice);
		srcCtrPanel.add(srcDel);
		srcCtrPanel.add(srcAllDel);
		srcCtrPanel.add(srcSetSub);
		srcCtrPanel.add(srcAllSetSub);		
		srcPanel.setBorder(BorderFactory.createTitledBorder("选择源文件(支持多选)"));
		
		//--
		srcChoice.addActionListener(this);
		srcDel.addActionListener(this);
		srcAllDel.addActionListener(this);
		srcSetSub.addActionListener(this);
		srcAllSetSub.addActionListener(this);
		//--
		destPanel.setLayout(new BoxLayout(destPanel,BoxLayout.Y_AXIS));
		destPanel.add(dest);
		dest.setEditable(false);
		dest.setCursor(new Cursor(Cursor.TEXT_CURSOR));
		destPanel.add(optionPanel);
		optionPanel.add(destChoice);
		for(int k=0;k<options.length;k++){
			options[k]=new JRadioButton(optionLabel[k]);
			destSaveMode.add(options[k]);	
			optionPanel.add(options[k]);
		}		
		options[0].setSelected(true);

		destPanel.setBorder(BorderFactory.createTitledBorder("选择目标文件夹"));
		destChoice.addActionListener(this);
		message.setForeground(Color.blue);//设置提示为蓝色
		msgPanel.add(message);
		ctrPanel.add(startCut);
		ctrPanel.add(quit);
		startCut.addActionListener(this);
		quit.addActionListener(this);	
		
		subBar=new JProgressBar(0,100);		
		subBar.setStringPainted(true);
		//subBar.setVisible(false);
		allBar=new JProgressBar(0,100);		
		allBar.setStringPainted(true);
		//allBar.setVisible(false);
		barPanel.setLayout(new BoxLayout(barPanel,BoxLayout.Y_AXIS));
		barPanel.add(subBarLabel);
		barPanel.add(subBar);
		barPanel.add(allBarLabel);
		barPanel.add(allBar);
		barPanel.setVisible(false);
		
		nextPanel.setLayout(new BoxLayout(nextPanel,BoxLayout.Y_AXIS));	
		nextPanel.add(destPanel);
		nextPanel.add(msgPanel);
		nextPanel.add(ctrPanel);
		nextPanel.add(barPanel);
		this.setLayout(new BorderLayout());	
		this.add(srcPanel,"Center");
		this.add(nextPanel,"South");
		
		this.setSize(700, 500);
		this.page=page;//需要回调页的设置,防止切割合并同时进行,破坏速度
		page.repaint();

	}
	/**
	 * @param src
	 * @param dest
	 */
	 	
	public void tableChanged(TableModelEvent e){//表值改变事件		
		if(src.getEditingRow()!=-1 && src.getEditingColumn()!=-1 && n++==1){ 
			try{			 
				FWsub=TableTableModel.FWsub;
				if (Integer.parseInt(myModel.getValueAt(src.getEditingRow(), 2).toString())>=2 && Integer.parseInt(myModel.getValueAt(src.getEditingRow(), 2).toString())<=9999){
					Long all_len=(Long)fileLen.get(src.getEditingRow());//文件总长度
					Long sub_len=all_len/Integer.parseInt(myModel.getValueAt(src.getEditingRow(), 2).toString());//块的大小
					subLen.set(src.getEditingRow(),sub_len);
					String subLength=(sub_len<1024*1024)?(Math.round(sub_len/1024.0*100.0)/100.0+"KB"):(Math.round(sub_len/1024.0/1024.0*100.0)/100.0+"MB");
					myModel.setValueAt(subLength,src.getEditingRow(),3);
				}
				else{
					myModel.setValueAt(FWsub, src.getEditingRow(), 2);
				}
			}catch (ClassCastException ex){
				System.err.println(ex.getStackTrace());
			}			
			catch (NumberFormatException ex){
				System.err.println(ex.getStackTrace());
			}

		}
		else
			n=1;
	}
	
	public void actionPerformed(ActionEvent e){//按钮事件
		if(e.getSource()==srcChoice){//选择源文件
			JFileChooser chooser=new JFileChooser();
			chooser.setMultiSelectionEnabled(true);//允许选择多个文件
			chooser.setDialogTitle("请选择欲切割的文件:");
			int result=chooser.showOpenDialog(this);
			if(result==JFileChooser.APPROVE_OPTION){				
				File []file=chooser.getSelectedFiles();	
				selectFilesNumber=file.length;//获得选择文件的个数
				fileString=new String[selectFilesNumber];//源文件列表
				int i=0;
				String fileLength=new String();//文件的字符串表示
				String subLength=new String();//块的字符串表示
				while(i++<selectFilesNumber){
					fileString[i-1]=file[i-1].getAbsolutePath();//文件名
					fileLen.addElement(new Long(file[i-1].length()));//文件长度				
					subLen.addElement(new Long(file[i-1].length()/nFile));//块长度
					fileLength=((Long)fileLen.lastElement()<1024*1024)?(Math.round((Long)fileLen.lastElement()/1024.0*100.0)/100.0+"KB"):(Math.round((Long)fileLen.lastElement()/1024.0/1024.0*100.0)/100.0+"MB");
					subLength=((Long)subLen.lastElement()<1024*1024)?(Math.round((Long)subLen.lastElement()/1024.0*100.0)/100.0+"KB"):(Math.round((Long)subLen.lastElement()/1024.0/1024.0*100.0)/100.0+"MB");
					myModel.addRow(new Object[] {
							fileString[i-1], fileLength, nFile, subLength});
				}
			}
		}
		else if (e.getSource()==srcDel){//删除
			if (src.getSelectedRows().length>0){
				if(FileSet.getDel()){//如果删除可用
					int getInt=JOptionPane.showConfirmDialog(this, "您确认删除所选选项吗?","请确认",JOptionPane.YES_NO_OPTION);
					if (getInt==JOptionPane.YES_OPTION){  
						delList();
					}
				}else{
					delList();
				}
			}else{
				JOptionPane.showMessageDialog(this, "选项为空!!!");
			}
		}
		else if (e.getSource()==srcAllDel){//全部删除
			if (src.getRowCount()>0){
				if(FileSet.getDel()){
					int getInt=JOptionPane.showConfirmDialog(this, "全部选项将被删除!确认吗?","请确认",JOptionPane.YES_NO_OPTION);
					if (getInt==JOptionPane.YES_OPTION){
						delAllList();
					}
				}else{
					delAllList();
				}
			}else{
				JOptionPane.showMessageDialog(this, "选项为空!!!");
			}
		}

⌨️ 快捷键说明

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