文件切割机

来源:互联网 发布:python smtplib 安装 编辑:程序博客网 时间:2024/04/27 15:46

文件切割与合并


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

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

程序说明:

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

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

文件合并:这里简单假设已知被合并目录的File对象和原文件的名字。其实这些完全可以做成活的,如把这些信息保存在碎片文件或配置文件,也可以同样用文件选择对话框

来读取用户的选择。

项目目的:

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

个人想法:

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

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

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

功能模块:用户点击分割或合并按钮,弹出文件选择框,分割时,只能选择一个文件,而合并时,设置可以选择多个文件,但是这个多个文件必须是同一类型的文件。分割

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

注意事项和问题:

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 blocke1.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()+"\r\n";}splitTextArea.setText(str);desDir = new File(files[0].getParent(),"mergeFiles"+1);try {mergeFile(files,desDir);} catch (IOException e1) {// TODO Auto-generated catch blocke1.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()){//如果路径存在,则修改路径,规则就是将文件后缀加1String 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+"\r\n";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;}}

运行截图:

开始:

分割文件:

分割完成:

分割碎片和存放:

合并文件:

合并完成:

合并文件存放:
















1 1
原创粉丝点击