Java做的文件分割合并工具

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JMenuItem;
import java.awt.Rectangle;
import javax.swing.JMenu;
import javax.swing.JToolBar;
import javax.swing.JMenuBar;
import javax.swing.JTabbedPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import java.util.*;
import javax.swing.JComboBox;;
public class Menu {
	static File cunDir=new File("d:\\");
	static File[] fl;
	static File[] chuanFile;
	//static File yFile;
	//static File fMuLu;
	static  File fenGeFile;
	static  File cunMuLu;
	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="64,31"
	private JTabbedPane jTabbedPane = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JTextField jTextField = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField1 = null;
	private JButton jButton1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField2 = null;
	private JLabel jLabel3 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	private JButton jButton4 = null;
	private JButton jButton5 = null;
	private JFileChooser jFileChooser = null;
	private JFileChooser jFileChooser1 = null;
	private JScrollPane jScrollPane = null;
	private JLabel jLabel4 = null;
	private JTextArea jTextArea = null;
	private JButton jButton6 = null;
	private JLabel jLabel41 = null;
	private JComboBox jComboBox = null;
	private JTextField jTextField3 = null;
	private JButton jButton7 = null;
	/**
	 * This method initializes jFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame("分割合并工具");
			jFrame.setSize(new Dimension(361, 274));
			jFrame.setContentPane(getJTabbedPane());
			jFrame.setVisible(true);
			jFrame.setLocation(300,300);
			jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
		}
		return jFrame;
	}

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("分割", getJPanel());
			jTabbedPane.addTab("合并", getJPanel1());
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(283, 106, 51, 18));
			jLabel3.setText("M");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(11, 103, 87, 28));
			jLabel2.setText("分割大小");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(11, 60, 83, 28));
			jLabel1.setText("存储目录");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("源文件");
			jLabel.setBounds(new Rectangle(11, 16, 76, 33));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(jLabel, gridBagConstraints);
			jPanel.add(getJTextField(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJTextField1(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJFileChooser(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getJTextField2(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(getJButton2(), null);
			jPanel.add(getJButton3(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel41 = new JLabel();
			jLabel41.setBounds(new Rectangle(164, 3, 108, 22));
			jLabel41.setText("合并后文件后缀:");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.add(getJButton4(), null);
			jPanel1.add(getJButton5(), null);
			jPanel1.add(getJFileChooser1(), null);
			jPanel1.add(getJScrollPane(), null);
			jPanel1.add(getJButton6(), null);
			jPanel1.add(jLabel41, null);
			jPanel1.add(getJComboBox(), null);
			jPanel1.add(getJTextField3(), null);
			jPanel1.add(getJButton7(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(105, 16, 165, 25));
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton("...");
			jButton.setBounds(new Rectangle(285, 16, 35, 30));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					jFileChooser.setMultiSelectionEnabled(false);
					jFileChooser.setFileSelectionMode(0);
					int state=jFileChooser.showOpenDialog(null);
					System.out.println("state="+state);
					//File fy=jFileChooser.getSelectedFile();
					if(state==0){
						fenGeFile=jFileChooser.getSelectedFile();
						jTextField.setText(fenGeFile.getAbsolutePath());
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(105, 60, 165, 25));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton("...");
			jButton1.setBounds(new Rectangle(285, 60, 35, 30));
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					jFileChooser.setMultiSelectionEnabled(false);
					jFileChooser.setFileSelectionMode(1);
					int state=jFileChooser.showOpenDialog(null);
					if(state==0){
						cunMuLu=jFileChooser.getSelectedFile();
						System.out.println("****"+cunMuLu.getAbsolutePath());
						jTextField1.setText(cunMuLu.getAbsolutePath());
					}
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField(5);
			jTextField2.setBounds(new Rectangle(105, 104, 165, 25));
		}
		return jTextField2;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton("分割");
			jButton2.setBounds(new Rectangle(11, 152, 135, 45));
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					int a=0;
					try{
						a=Integer.parseInt(jTextField2.getText());
					}catch(Exception ee){
						System.out.println("erro");
						return;
					}
					FengGeHeBing.fenGe(fenGeFile, cunMuLu, a);
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton("退出");
			jButton3.setBounds(new Rectangle(191, 152, 135, 45));
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					System.exit(0);
				}
			});
		}
		return jButton3;
	}

	/**
	 * This method initializes jButton4	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton("打开");
			jButton4.setBounds(new Rectangle(8, 155, 85, 35));
			jButton4.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFileChooser1.setFileSelectionMode(0);
					jFileChooser1.setMultiSelectionEnabled(true);
					int state=jFileChooser1.showOpenDialog(null);
					String s="";
					if(state==0){
						fl=jFileChooser1.getSelectedFiles();
						int[] st=new int[fl.length];
						for(int i=0;i<fl.length;i++){
							String name=fl[i].getName();
							int index=name.indexOf('.');
							st[i]=Integer.parseInt(name.substring(0,index));
							s=s+fl[i].getName()+"\r\n";
							jTextArea.setText(s);
						}
						Arrays.sort(st);
						chuanFile=new File[st.length];
						for(int i=0;i<st.length;i++){
							chuanFile[i]=new File(fl[i].getParent()+"\\"+(i+1)+".tem");
						}
					}
				}
			});
		}
		return jButton4;
	}

	/**
	 * This method initializes jButton5	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton5() {
		if (jButton5 == null) {
			jButton5 = new JButton("合并");
			jButton5.setBounds(new Rectangle(113, 155, 85, 35));
			jButton5.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					FengGeHeBing.heBing(chuanFile,cunDir,jComboBox.getSelectedItem().toString());
					jTextArea.setForeground(Color.red);
					jTextArea.setText("合并已完成");
				}
			});
		}
		return jButton5;
	}

	/**
	 * @param args
	 */
	public Menu(){
		this.getJFrame();
	}
	/**
	 * This method initializes jFileChooser	
	 * 	
	 * @return javax.swing.JFileChooser	
	 */
	private JFileChooser getJFileChooser() {
		if (jFileChooser == null) {
			jFileChooser = new JFileChooser();
			jFileChooser.setBounds(new Rectangle(5, 204, 500, 326));
		}
		return jFileChooser;
	}

	/**
	 * This method initializes jFileChooser1	
	 * 	
	 * @return javax.swing.JFileChooser	
	 */
	private JFileChooser getJFileChooser1() {
		if (jFileChooser1 == null) {
			jFileChooser1 = new JFileChooser();
			jFileChooser1.setFileSelectionMode(0);
			jFileChooser1.setBounds(new Rectangle(5, 193, 500, 326));
		}
		return jFileChooser1;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			jScrollPane.setBounds(new Rectangle(15, 34, 325, 105));
			jScrollPane.setViewportView(getJTextArea());
			
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}

	/**
	 * This method initializes jButton6	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton6() {
		if (jButton6 == null) {
			jButton6 = new JButton();
			jButton6.setBounds(new Rectangle(227, 155, 85, 35));
			jButton6.setText("退出");
			jButton6.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return jButton6;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setBounds(new Rectangle(276, 5, 69, 18));
			jComboBox.addItem(".exe");
			jComboBox.addItem(".pdf");
			jComboBox.addItem(".rm");
			jComboBox.addItem(".rmvb");
			jComboBox.addItem(".avi");
			jComboBox.setEditable(true);
		}
		return jComboBox;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
			jTextField3.setText(cunDir.getAbsolutePath());
			jTextField3.setBounds(new Rectangle(110, 5, 50, 22));
		}
		return jTextField3;
	}

	/**
	 * This method initializes jButton7	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton7() {
		if (jButton7 == null) {
			jButton7 = new JButton();
			jButton7.setBounds(new Rectangle(1, 6, 105, 18));
			jButton7.setText("存放目录:");
			jButton7.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFileChooser1.setFileSelectionMode(1);
					jFileChooser1.setMultiSelectionEnabled(false);
					int state=jFileChooser1.showOpenDialog(null);
					if(state==0){
						cunDir=jFileChooser1.getSelectedFile();
						jTextField3.setText(cunDir.getAbsolutePath());
					}
				}
			});
		}
		return jButton7;
	}

	public static void main(String[] args) {
		new Menu();
	}

}

 

import java.io.*;
import java.util.*;
public class FengGeHeBing {
	static int nM=1024*1024;
	static public void fenGe(File fenGeFile,File cunMuLu,int daXiao){
		FileInputStream fis=null;
		try{
			if(!cunMuLu.isDirectory()){
				cunMuLu.mkdirs();
			}
			nM=nM*daXiao;
			int length=(int)fenGeFile.length();
			int num=length/nM;
			int yu=length%nM;
			System.out.println("feGenFile.length:"+length);
			fis=new FileInputStream(fenGeFile);
			//byte[] yByte=new byte[length];
			//fis.read(yByte);
			int wenJianShu=0;
			File[] fl=new File[num+1];
			int begin=0;
			for(int i=0;i<num;i++){
				fl[i]=new File(cunMuLu.getAbsolutePath()+"\\"+(i+1)+".tem");
				if(!fl[i].isFile()){
					fl[i].createNewFile();
				}
				FileOutputStream fos=new FileOutputStream(fl[i]);
				byte[] bl=new byte[nM];
				fis.read(bl);
				//fos.write(yByte,begin,daXiao*1024*1024);
				fos.write(bl);
				begin=begin+daXiao*1024*1024;
				fos.close();
				//System.out.println(fl[i].getName()+"  length"+fl[i].length());
			}
			if(yu!=0){
				fl[num]=new File(cunMuLu.getAbsolutePath()+"\\"+(num+1)+".tem");
				if(!fl[num].isFile()){
					fl[num].createNewFile();
				}
				FileOutputStream fyu=new FileOutputStream(fl[num]);
				byte[] bl=new byte[yu];
				fis.read(bl);
				//fyu.write(yByte,length-yu,yu);
				fyu.write(bl);
				fyu.close();
				//System.out.println(fl[num].getName()+"  length"+fl[num].length());
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			//fis.close();
		}
	}
	public static void heBing(File[] f,File cunDir,String hz) {
		try {
			//File[] fl = f.listFiles();
			File heBingFile = new File(cunDir.getAbsoluteFile()+"\\heBing"+hz);
			if (!heBingFile.isFile()) {
				heBingFile.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(heBingFile);
			for (int i = 0; i < f.length; i++) {
				FileInputStream fis = new FileInputStream(f[i]);
				int len = (int) f[i].length();
				byte[] bRead = new byte[len];
				fis.read(bRead);
				fos.write(bRead);
				fis.close();
			}
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args){
		File fenGeFile=new File("d:\\《金庸全集》典藏版v2.1.exe");
		File ccMuLu=new File("d:\\fenGe\\");
		int daXiao=3;
		//fenGe(fenGeFile,ccMuLu,daXiao);
		File heBingFile=new File("d:\\fenGe\\");
		//heBing(heBingFile);
	}
}

 

第一个java文件主要写界面,第二个java文件写分割合并应用。老婆给我禁魔,自娱而已。

你可能感兴趣的:(java,swing,F#)