日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文件分割机

發布時間:2024/9/21 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件分割机 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文件分割與合并


要求:實現對大文件的分割與合并。

按指定個數切(如把一個文件切成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 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;}}

執行截圖:

開始:

切割文件:

切割完畢:

切割碎片和存放:

合并文件:

合并完畢:

合并文件存放:
















總結

以上是生活随笔為你收集整理的文件分割机的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。