Use java.io. * Operating file split and merge, an example of Application!

zhaozj2021-02-17  45

Import javax.swing. *; import java.awt. *; import java.awt.event. *; import java.util. *; import java.io. *; import javax.swing.text. *; import javax.swing .

public class Application1 extends JFrame {String filepath = ""; JLabel splitSourceSize, coalitionTotalSize, coalitionTotalCount; JTextField SplitSource, SplitTotalCount, SplitEachSize, coalitionSource; // ******************** **************************** The end public application1 () {Super (Tool "); this.setsize (640, 480); this.setLocation (this.gettoolkit) ). GetScreensize (). width / 2-this.getwidth () / 2, this.gettoolkit (). getScreensize (). Height / 2-this.getHEight () / 2); jPanel ContentPanel = (jPanel) this.getContentPane (); contentPanel.setLayout (new BorderLayout ()); JTabbedPane jtp = new JTabbedPane (); jtp.setTabLayoutPolicy (JTabbedPane.SCROLL_TAB_LAYOUT); JPanel splitPane = new JPanel (new GridLayout (5,1)); JPanel splitPane0 = new JPanel (new BorderLayout ()); splitPane0.add (splitPane, BorderLayout.NORTH); JPanel splitPane1 = new JPanel (new FlowLayout (FlowLayout.LEADING)); JPanel splitPane2 = new JPanel (new FlowLayout (FlowLayout.LEADING)); JPanel splitPane3 = New flowLayout (FlowLayout.Leading); JPANel splitpane4 = new jPanel (New flowLayout (f LowLayout.Lleading); JPanel splitpane5 = new jPanel (); splitpane1.add (New Jlabel ("Source file:")); splitsource = new jtextfield (40); splitpane1.add (splitsource); jButton splitjbutton = new JButton ("Browse ..."); splitjbutton.addActionListener (new actionListener () {public void actionerformed (splitjbuttonclick (e); // Select file}}} to split}}); splitpane1.add (splitjbutton) SplitsourceSize = New Jlabel ("Source File Size:"); splitpane2.add (splitSourceSize); splitpane3.add (New Jlabel: Splittotalcount = New JtextField (15); splittotalcount.SetDocument NEW numberDocument ());

// limit input box can only enter numbers SplitTotalCount.getDocument (). AddDocumentListener (new DocumentListener () {// input box to add the event public void changedUpdate (DocumentEvent e) {if (! Application1.this.SplitTotalCount.isFocusOwner ()) return; try {SplitTotalCount_changedUpdate (e);} catch (Exception ex) {System.err.println (ex.toString ());}!} public void insertUpdate (DocumentEvent e) {if (Application1.this.SplitTotalCount.isFocusOwner ( )) return; try {SplitTotalCount_changedUpdate (e);} catch (Exception ex) {System.err.println (ex.toString ());}} public void removeUpdate (DocumentEvent e) {if (Application1.this.SplitTotalCount!. isFocusOwner ()) return; try {SplitTotalCount_changedUpdate (e);} catch (Exception ex) {System.err.println (ex.toString ());}}}); splitPane3.add (SplitTotalCount); splitPane4.add (new JLabel ( "single file size:")); SplitEachSize = new JTextField (15); SplitEachSize.setDocument (new NumberDocument ()); // only numeric limit input box SplitEachSize.getDocument () addDocumentListener (new DocumentListener (.) {/ {Add events to the input box public void changeupdate (DocumentEvent e) {(! Application1.this.SplitEachSize.isFocusOwner ()) if return; try {SplitEachSize_changedUpdate (e);} catch (Exception ex) {System.err.println (ex.toString ());}} public (! Application1.this.SplitEachSize.isFocusOwner ()) void insertUpdate (DocumentEvent e) {if return; try {SplitEachSize_changedUpdate (e);} catch (Exception ex) {System.err.println (ex.toString ());} } public void removeUpdate (DocumentEvent e) {(! Application1.this.SplitEachSize.isFocusOwner ()) if return; try {SplitEachSize_changedUpdate (e);} catch (Exception ex) {System.err.println (ex.toString ()) ;}}}); Splitpane4.add (spliteachsize); jbutton splitjb = new jbutton ("Start Split");

Splitjb.addActionListener (New ActionListener () {PUBLIC VOID ActionPerformed (ActionEvent E) {try {splitjbclick (e);} catch (exception ex) {system.err.println (ex. scroll ());} // split file }}); splitPane5.add (SplitJB); splitPane.add (splitPane1); splitPane.add (splitPane2); splitPane.add (splitPane3); splitPane.add (splitPane4); splitPane.add (splitPane5); JPanel coalitionPane0 = new JPanel (new BorderLayout ()); JPanel coalitionPane = new JPanel (new GridLayout (4,1)); coalitionPane0.add (coalitionPane, BorderLayout.NORTH); JPanel coalitionPane1 = new JPanel (new FlowLayout (FlowLayout.LEADING)); JPanel coalitionPane2 = new JPanel (new FlowLayout (FlowLayout.LEADING)); JPanel coalitionPane3 = new JPanel (new FlowLayout (FlowLayout.LEADING)); JPanel coalitionPane4 = new JPanel (); coalitionPane1.add (new JLabel ( "merge first file:" )); coalitionSource = new JTextField (40); coalitionPane1.add (coalitionSource); JButton coalitionJButton = new JButton ( "Browse (* .000)"); coalitionJButton.addActionListener (new ActionListener () {public void actionPerformed (ActionEven TE) {coalitionjButtonClick (e); // Select the first file}}}}}) to merge; coalitionpane.add (coalitionpane.add (coalitionpane1); coalitionTotalsize = New Jlabel ("file total size:"); coaallyPane2.add (coalitionTotalSize); coalitionPane.add (coalitionPane2); coalitionTotalCount = new JLabel ( "total number of files:"); coalitionPane3.add (coalitionTotalCount); coalitionPane.add (coalitionPane3); JButton coalitionJB = new JButton ( "start merge"); Coalitionjb.addActionListener (New ActionListener () {PUBLIC VOID ActionPerformed (ActionEvent E) {Try {CoalitionJbClick (e);} catch (exception ex) {system.err.println (ex. scroll ());} // Merge file} }); coalitionpane4.add (coalitionjb); coalitionpane.add (coalitionpane4); jtp.add ("

Split File ", splitPane0); jtp.add (" merge file ", coalitionPane0); contentPanel.add (jtp, BorderLayout.CENTER); this.addWindowListener (new WindowAdapter () {public void windowClosing (WindowEvent e) {System. EXIT (0); // Exit system}} when the window is turned off; this.setvisible (true); // *********************** ******************* More than graphical interface design}

Private Void SplitjbClick (ActionEvent E) THROWS Exception {// Split File Operation File = New File (this.Splitsource.getText ()); if (! file.exists ()) {joptionpane.showMessageDialog (this, "source file does not exist "," warning ", JOptionPane.WARNING_MESSAGE);! return;} javax.swing.JFileChooser fjc; fjc = new javax.swing.JFileChooser (filepath); fjc.removeChoosableFileFilter (fjc.getAcceptAllFileFilter ()); fjc.setDialogTitle (Select Save Directory "); fjc.setFileSelectionMode (jfilechooser.directories_only); if (fjc.Approve_Option! = Fjc.showopendialog (this)) Return; filepath = fjc.getSelectedFile (). GetPath (); byte [] b = new byte [1024]; int read = 0; String splitfilename = file.getName (); int eachfilesize = Integer.parseInt (this.SplitEachSize.getText ()); FileInputStream fis = new FileInputStream (file); FileOutputStream fos; String savesplitfilename = ""; for (long splitcount = 0; splitcount 0) {Fos.Write (B, 0, READ); FOS .flush (); haasread = read; if (Hasread> = Eachfilesize && splitcount! = long.parselong (this.Splittotalcount.getText ()) - 1) Break;

IF (Eachfilesize-Hasread <(long) B.Length) Eachread = Eachfilesize-Hasread;} ​​fos.close ();} fos = null; joptionpane.showMessageDialog (this, Split ");}

Private Void CoalitionJbClick (ActionEvent E) THROWS Exception {// Merge File Operation File = New File (this.coalition.getText ()); if (! file.exists ()) {JOPAANE.SHOWMESSAGEDIALOG (this, "source file Presented! " JFileChooser.DIRECTORIES_ONLY); if (! fjc.APPROVE_OPTION = fjc.showOpenDialog (this)) return; filepath = fjc.getSelectedFile () getPath ();. String selfilename = file.getName (); selfilename = selfilename.substring (0, Selfilename.lastIndexof (".")); string savefile = fjc.getSelectedFile (). getabsolutePath () "/" selfilename.substring (0, selfilename.lastIndexof ("."); if (new file (savefile) .exists ()) {int Javaok = JOTIONPANE.SHOWCONFIRMDIALOG (this, "File Name" new file (savefile). GetName () "file already exists, do you want to overwrite the original file?", "Warning!" , JOPTIONPANE.OK_CANCEL_OPTION; if (javaok == joptionpane.cancel_option) return;} byte [] b = new byte [1024]; int = 0; FileOutputStream Fos = new fileoutputstream (Savefile); fileinputstream fis; file [] f = file.getParentFile (). ListFiles (); for (int i = 0; i 0) {fos.write (B , 0, read); fos.close ();} fos.close (); fos.close (); fos = null; fis = null; joptionpane.showMessageDialog (this, "merge");}

private void SplitTotalCount_changedUpdate (DocumentEvent e) throws Exception {File file = new File (this.SplitSource.getText ()); if (!. file.exists () || this.SplitTotalCount.getText () equals ( "")) return Long L = long.parselong (this.splittotalcount.getText ()); if (l <= 0) return; this.SpliteachSize.setText (String.Valueof (file.Length () / L));} private void spliteachsize_changedupdate (DocumentEvent E) THROWS Exception {file file = new file (this.splitsource.gettext ()); if (! File.exists () || this.spliteachsize.getText (). Equals (")) Return; Double L = Double.PARSEDOUBLE (this.spliteachsize.getText ()); if (l <= 0) return; this.splittotalcount.setText (FileCount "file.length (), l));}

private void SplitJButtonClick (ActionEvent e) {// choose to split the source file javax.swing.JFileChooser fjc; fjc = new javax.swing.JFileChooser (filepath); fjc.addChoosableFileFilter (new myFilter ( "* jpg, *.. GIF "," JPG, GIF Files ")); // File Filter Fjc.AddchoosableFileFilter (New Myfilter (" *. RM, *. RMVB "," Real Files (*. RM, * .rmvb) ")); // File Filter FJC.AddchoosableFileFilter (New Myfilter (" *. MPG, *. MPEG "," MPG Files (*. MPG, *. MPEG) "); // File Filter FJC .addChoosableFileFilter (fjc.getAcceptAllFileFilter ()); if (! fjc.APPROVE_OPTION = fjc.showOpenDialog (this)) return; filepath = fjc.getSelectedFile () getPath ();. File file = fjc.getSelectedFile (); double filesize = File.Length (); string size = ""; java.text.decimalformat df = new java.text.decimalformat ("#. ##"); if (filesize> = 1024d * 1024d * 0.8d) size = DF. Format (FileSize / (1024D * 1024D)) "MB"; Else IF (FileSize> = 1024D * 0.8d) size = df.format (FileSize / 1024D) "KB"; else size = filesize "bytes"; THIS .splitsourcesize.settext ("Source file size:" size); this.splitsource.settext (file.getabsolutePath ()); TH Is.splittotalcount.Settext (Filecount (FileSize, 102400); this.spliteachsize.settext ("102400");}

private void coalitionJButtonClick (ActionEvent e) {// choose to merge the first document javax.swing.JFileChooser fjc; fjc = new javax.swing.JFileChooser (filepath); fjc.removeChoosableFileFilter (fjc.getAcceptAllFileFilter ()); // removed Default select all fjc.addchoosablefilefilter (new myfilter ("*. Xiruo.000)); // File filtering if (fjc.Approve_option! = Fjc.showopendialog this)) return; filepath = fjc.getSelectedFile () getPath ();. File file = fjc.getSelectedFile (); String selfilename = file.getName (); this.coalitionSource.setText (file.getAbsolutePath ()); selfilename = Selfilename.substring (0, selfilename.lastindexof (".")); file [] f = file.getParentFile (). listfiles (); double filesis = 0; int filetotalcount = 0; for (int i = 0; i < F.Length; i ) {if (! f [i] .GetName (). StartSwith (Selfilename)) Continue; FileTotalcount ; FileSize = f [i] .length ();} string size = "; java.text. Decimalformat DF = New java.text.decimalformat ("#. ##"); if (filesize> = 1024d * 1024d * 0.8d) size = DF.FORMAT (FileSize / (1024D * 1024D)) "MB"; ELSE IF (Filesi Ze> = 1024d * 0.8d) size = df.format (filesize / 1024d) "kb"; else size = filesis "bytes"; this.coalitionTotalsize.Settext ("file total size: size); this.coalitionTotalcount .SETTEXT ("Number of files:" filetotalcount); String.Valueof (i);

public class NumberDocument extends PlainDocument {// input text box input limits configured PlainDocument achieve public void insertString (int offs, String str, AttributeSet a) throws BadLocationException {char [] source = str.toCharArray (); char [] Result = new char [Source.Length]; INT J = 0; for (int i = 0; i

, font); UIManager.Put ("textfield.font", font; uimanager.put ("passwordfiled.font", font); UIManager.put ("textarea.font", font); UIManager.put ("TextPane. Font ", font); UIManager.put (" editorpane.font ", font); UIManager.put (" Titledborder.Font ", font); UIManager.put (" Toolbar.Font ", font); uimanager.put (" Tooltip.font ", font); UIManager.put (" Tree.Font ", font); // The setting is to solve Chinese issues uimanager.seltLookandFeel (uimanager.getsystemLookandFeelClassName ()); // Set a visual style new application1 ( );}} // ************************************************************ **** file selection filter class myFilter extends javax.swing.filechooser.FileFilter {String extension = "", description = ""; public myFilter (String extension, String description) {if (! extension = null) this.extension = extension; if (description! = null) this.description = description;} public string getdescription;} {return this.description;}

Public Boolean Accept (file file) {if (file.isdirectory () || extension.equals (")) Return true; string [] s = extension.replaceAll (" [*], ""). split ("" [,] "); for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-28530.html

New Post(0)