zoukankan      html  css  js  c++  java
  • 文件分割机

    文件分割与合并


    要求:实现对大文件的分割与合并。

    按指定个数切(如把一个文件切成10份)或按指定大小切(如每份最大不超过10M),这两种方式都能够。

    程序说明:

    文件分割:把一个文件分割成多个碎片,每一个碎片的大小不超过1M。自己可把功能进一步扩展:分割前的文件名称、长度,分割后的碎片个数、文件名称等信息可写到第一个碎

    片中或另外用properties把这些写到配置文件里。

    文件合并:这里简单如果已知被合并文件夹的File对象和原文件的名字。

    事实上这些全然能够做成活的,如把这些信息保存在碎片文件或配置文件。也能够相同用文件选择对话框

    来读取用户的选择。

    项目目的:

    做一个简单的图形界面的文件处理器。能实现对单个文件的分割。和将多个文件合而唯一的功能。

    个人想法:

    本着做个简单的图形界面的想法,所以没有过的美化界面。就是简单的实现功能。

    图形界面模块:两个选择button:分隔操作还是合并操作;一个退出button;两个文本域。一个显示要切割的文件和合成后的文件,还有一个显示切割后的文件和要合成的文件;

    两个文本显示框,分别在两个文本域以下,显示文本域中文件的路径。(还有略微好点的界面就是用户先选择要切割和合并的文件,然后在选择要存储的位置,最后点操作button)

    功能模块:用户点击切割或合并button,弹出文件选择框,切割时。仅仅能选择一个文件,而合并时,设置能够选择多个文件,可是这个多个文件必须是同一类型的文件。切割

    后和合并后的文件都应与相应操作前的文件类型同样。

    注意事项和问题:

    1、图形界面看个人喜好,能够自己设置,但个人在布局上面喜欢用this.setLayout(null);然后通过setBounds设置绝对位置

    2、要是实现可选择存储位置时,记得用jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);设置成仅仅选择文件夹,且要将这句放在jfc.showOpenDialog(null)前

    面,不然无效哦

    3、关于分割和合并后的文件的存放和文件名称问题值得深究

    4、合并时。选中的多个文件类型要求同样

    5、文件的切割和合并(解说见io基础到加强)

    解决这个问题方案:

    主要是注意事项和问题中的3。首先关于路径。能够在选中的文件的同级目录下建立splitFile1或mergeFile1目录,然后通过检查是否存在这种目录,若存在则取出那

    两个文件的后缀为数组的部分,然后将加1。再加在splitFIle或mergeFile后面。就可以避免存放路径反复。由于有了前面的处理,所以仅仅要进行一次操作,只是是切割还是合并。

    都会又一次建立一个不存在的目录存放结果文件。并且合并时,还能够将合并的分文件也同一时候拷贝都结果目录中。

    大概就是这么回事啊,还有非常多细节问题有待更新啊!

    程序代码:

    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.SequenceInputStream;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Enumeration;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class FileSplitDemo extends JFrame implements ActionListener{
    	JLabel titleLabel,resultLabel,dirLabel;
    	JTextField sdirTextField,mdirTextField;
    	JButton splitButton,mergeButton,exitJButton;
    	JTextArea mergeTextArea,splitTextArea;
    	JScrollPane jsp1,jsp2;
    	JFileChooser jfc;
    	File dirFile;
    	static int mergeCount;
    	//图型界面设置
    	public FileSplitDemo(){
    		super("文件处理器");
    		mergeCount=0;
    		this.setSize(400, 500);
    		this.setLocationRelativeTo(null);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setResizable(false);
    		this.setLayout(null);
    		titleLabel=new JLabel("请选择操作种类:");
    		titleLabel.setBounds(10, 10, 100, 50);
    		splitButton=new JButton("分割文件");
    		splitButton.setBounds(50, 50, 100, 30);
    		mergeButton=new JButton("合成文件");
    		mergeButton.setBounds(230, 50, 100, 30);
    		mergeTextArea=new JTextArea(10, 10);
    		mergeTextArea.setEditable(false);
    		resultLabel=new JLabel();
    		resultLabel.setBounds(165, 180, 100, 50);
    		dirLabel=new JLabel();
    		dirLabel.setBounds(140, 200, 100, 100);
    		sdirTextField=new JTextField();
    		sdirTextField.setEditable(false);
    		sdirTextField.setBounds(10, 400, 150, 30);
    		mdirTextField=new JTextField();
    		mdirTextField.setEditable(false);
    		mdirTextField.setBounds(220, 400, 150, 30);
    		exitJButton=new JButton("退出");
    		exitJButton.setBounds(140, 430, 100, 30);
    		exitJButton.addActionListener(this);
    		jsp1=new JScrollPane(mergeTextArea);
    		jsp1.setBounds(10, 90, 150, 300);
    		splitTextArea=new JTextArea(10, 10);
    		splitTextArea.setEditable(false);
    		jsp2=new JScrollPane(splitTextArea);
    		jsp2.setBounds(220, 90, 150, 300);
    		this.getContentPane().add(titleLabel);
    		this.getContentPane().add(mergeButton);
    		this.getContentPane().add(splitButton);
    		this.getContentPane().add(jsp1);
    		this.getContentPane().add(jsp2);
    		this.getContentPane().add(resultLabel);
    		this.getContentPane().add(dirLabel);
    		this.getContentPane().add(sdirTextField);
    		this.getContentPane().add(mdirTextField);
    		this.getContentPane().add(exitJButton);
    		splitButton.addActionListener(this);
    		mergeButton.addActionListener(this);
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		new FileSplitDemo();
    
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == splitButton){
    			jfc = new JFileChooser();
    			jfc.setDialogTitle("请选择一个要分割的文件");
    			int result = jfc.showOpenDialog(this);
    			File file =null;
    			File desDir =null;
    			//1分割
    			if(result==JFileChooser.APPROVE_OPTION){
    				//分割文件
    				file = jfc.getSelectedFile();//用户所选择的文件
    				mergeTextArea.setText(file.getName());
    				desDir = new File(file.getParent(),"splitFiles"+1);
    //				System.out.println(desDir.getAbsolutePath());
    				try {
    					fileSplit(file,desDir);
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				dirFile=jfc.getCurrentDirectory();
    				sdirTextField.setText(dirFile.getPath());
    				mdirTextField.setText(desDir.getPath());
    				resultLabel.setText("分割结果");
    				dirLabel.setIcon(new ImageIcon("right.png"));
    			}
    		}
    		if (e.getSource() == mergeButton){
    			jfc = new JFileChooser();
    			jfc.setDialogTitle("请选择若干个要合成的文件");
    			jfc.setMultiSelectionEnabled(true);
    			int result = jfc.showOpenDialog(this);
    			File[] files =null;
    			File desDir =null;
    			//合成
    			if(result==JFileChooser.APPROVE_OPTION){
    				files = jfc.getSelectedFiles();
    				if(!isLegal(files)){//推断是否存在种文件
    					JOptionPane.showMessageDialog(this, "请选择同一类型文件!!");
    					return;
    				}
    				String str="";
    				for(int i=0;i<files.length;i++){
    					str+=files[i].getName()+"
    ";
    				}
    				splitTextArea.setText(str);
    				desDir = new File(files[0].getParent(),"mergeFiles"+1);
    				try {
    					mergeFile(files,desDir);
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    
    				dirFile=jfc.getCurrentDirectory();
    				sdirTextField.setText(dirFile.getPath());
    				mdirTextField.setText(dirFile.getPath());
    				resultLabel.setText("合成结果");
    				dirLabel.setIcon(new ImageIcon("left.png"));
    			}
    		}
    		if(e.getSource()==exitJButton){
    			System.exit(EXIT_ON_CLOSE);
    		}
    	}
    
    	private boolean isLegal(File[] files) {
    		String s=" ",a;
    		int count=0;
    		for(int i=0;i<files.length;i++){
    			a=files[i].getName();
    			int j=a.lastIndexOf('.');
    			String str=a.substring(j+1);
    			if(s.compareToIgnoreCase(str)!=0){
    				s=str;
    				count++;
    			}
    			if(count>1){
    				return false;
    			}
    		}
    		return true;
    	}
    
    	private void mergeFile(File[] files, File srcDir) throws IOException{
    		if(files.length==0){
    			throw new RuntimeException("碎片不存在。");
    		}
    		while(srcDir.exists()){//假设路径存在。则改动路径,规则就是将文件后缀加1
    			String s=getName(srcDir.getName());
    			srcDir=new File(srcDir.getParent(),s);
    		}
    		srcDir.mkdirs();
    		//System.out.println(srcDir.getParent()+" "+srcDir.getAbsoluteFile());
    		fileCopy(files,srcDir.getParent(),srcDir.getAbsoluteFile());
    		//用序列流进行文件合并
    		ArrayList<FileInputStream> aList = new ArrayList<FileInputStream>();
    		for(int i=0;i<files.length;i++){
    			aList.add(new FileInputStream( files[i]) );
    		}
    		//枚举接口对象
    		Enumeration<FileInputStream> en = Collections.enumeration(aList);
    		SequenceInputStream sis = new SequenceInputStream(en);
    		
    		//把序列流其中的内容写到一个新文件(合并后的文件)
    		int a=files[0].getName().lastIndexOf('.');
    		String s="megreFile"+files[0].getName().substring(a);
    		mergeTextArea.setText(s);
    		FileOutputStream fos = new FileOutputStream(new File(srcDir,s));
    		byte buf[] = new byte[1024];
    		int len=0;
    		while( (len=sis.read(buf))!=-1){
    			fos.write(buf,0,len);
    		}
    		fos.close();
    		sis.close();
    	}
    
    	private void fileCopy(File[] files, String dir1, File dir2) {
    		System.out.println(dir1+" "+dir2);
    		BufferedInputStream in = null;
    		BufferedOutputStream out = null;
    		for (int j = 0; j < files.length; j++) {
    			try {
    				in = new BufferedInputStream(new FileInputStream(files[j]));
    				out = new BufferedOutputStream(new FileOutputStream(new File(dir2,files[j].getName())));
    				byte[] buffer = new byte[512];
    				int num = 0;
    				while (in.available() > 0) {
    					num = in.read(buffer);
    
    					//最简单的加密
    					for (int i = 0; i < num; i++) {
    						buffer[i] = (byte) (buffer[i] + 1);
    					}
    
    					out.write(buffer, 0, num);
    				}
    
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    			} catch (Exception e) {
    			} finally {
    				try {
    					in.close();
    					out.close();
    				} catch (IOException e) {
    					throw new RuntimeException("文件无法关闭");
    				}
    			}
    		}
    	}
    
    	private void fileSplit(File srcFile, File desDir) throws IOException{
    		//1源
    		FileInputStream fis = new FileInputStream(srcFile);
    		//2目的
    		while(desDir.exists()){
    			String s=getName(desDir.getName());
    			desDir=new File(desDir.getParent(),s);
    		}
    		desDir.mkdirs();
    		//分割
    		FileOutputStream fos = null;
    		byte buf[] = new byte[1024*1024];
    		int len=0;
    		int count=1;
    		String s="";
    		while( (len=fis.read(buf))!=-1 ){
    			int a=srcFile.getName().lastIndexOf('.');
    			String fileName = srcFile.getName().substring(0,a)+(count++)+srcFile.getName().substring(a);
    			s+=fileName+"
    ";
    			fos = new FileOutputStream( new File(desDir,fileName) );
    			fos.write(buf,0,len);
    			fos.close();
    		}
    		splitTextArea.setText(s);
    	}
    
    	private String getName(String name) {
    		int k=0;
    		for(int i=0;i<name.length();i++){
    			if(name.charAt(i)>='0'&&name.charAt(i)<='9'){
    				k=i;
    				break;
    			}
    		}
    		String s=name.substring(k,name.length());
    		int a=Integer.parseInt(s)+1;
    		return name.substring(0, k)+a;
    	}
    	
    
    }
    

    执行截图:

    開始:

    切割文件:

    切割完毕:

    切割碎片和存放:

    合并文件:

    合并完毕:

    合并文件存放:
















  • 相关阅读:
    使用Apache搭建个人用户主页
    Linux搭建ftp服务器,并建立本地用户与虚拟用户
    Linux搭建FTP服务器,并建立匿名用户登录
    用虚拟机安装RHEL7
    基于OpenStack构建企业私有云(5)Neutron
    基于OpenStack构建企业私有云(3)Glance
    基于OpenStack构建企业私有云(1)实验环境准备
    基于OpenStack构建企业私有云(2)KeyStone
    python--003--百分号字符串拼接、format
    python--002--数据类型(set)
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7394510.html
Copyright © 2011-2022 走看看