以下是源码: import java.awt.Panel; import java.awt.TextField; import java.awt.Frame; import java.awt.Button; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.ButtonGroup; import javax.swing.JFileChooser; import javax.swing.border.TitledBorder; import java.awt.event.ActionListener; import javax.swing.JSplitPane; import javax.swing.UIManager; import javax.swing.JOptionPane; import javax.swing.Box; import javax.swing.JFrame; import java.io.File; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; public class SplitFiles extends JFrame implements ActionListener { private JTextField showSourcePath=new JTextField(); private JTextField showDestinationPath=new JTextField(); private JButton source=new JButton("源文件"); private JButton destination=new JButton("目标文件夹"); private JButton start=new JButton("确定"); private JRadioButton splitByAvg=new JRadioButton("平均分割"); private JRadioButton splitBySize=new JRadioButton("指定大小分割"); private JTextField showAvg=new JTextField(); private JLabel showLiang1=new JLabel("分割成?份",JLabel.CENTER); private JTextField showSize=new JTextField(""); private JLabel showLiang2=new JLabel("每份/KB",JLabel.CENTER); private JButton aboutUs=new JButton("关于我们"); private String glz; private JFileChooser fileChooser; private File sourceFile; private File destinationFile; private String destinationPath; private int avgNum; private int sizeNum; public SplitFiles() { ButtonGroup g=new ButtonGroup(); g.add(splitByAvg); g.add(splitBySize); glz="有关于我们的最新情况请访问:www.ourglz.com"+"/n"+"您可以在此讨论任何学习&生活中的事情" +"/n"+"我的邮箱:494763134@qq.com"+"/n"+"我的qq:494763134"+"/n"+"/t"+"---glzlaohuai"+"/n"+"/t"+"2010.4.18"; showSourcePath.setEditable(false); showDestinationPath.setEditable(false); splitByAvg.setSelected(true); showAvg.setEditable(true); showSize.setEditable(false); JPanel p1=new JPanel(); p1.setLayout(new BorderLayout()); p1.add(showSourcePath,BorderLayout.CENTER); JPanel p2=new JPanel(); p2.setLayout(new BorderLayout()); p2.add(showDestinationPath,BorderLayout.CENTER); Box b1=Box.createHorizontalBox(); b1.add(p1); b1.add(Box.createGlue()); b1.add(source); b1.setBorder(new TitledBorder("选择源文件")); Box b2=Box.createHorizontalBox(); b2.add(p2); b2.add(Box.createGlue()); b2.add(destination); b2.setBorder(new TitledBorder("选择目标文件夹")); JPanel p3=new JPanel(); p3.setLayout(new GridLayout(2,1)); p3.add(b1); p3.add(b2); JPanel p9=new JPanel(); p9.setLayout(new FlowLayout()); p9.add(start); JPanel p4=new JPanel(); p4.setLayout(new BorderLayout()); p4.add(p3,BorderLayout.CENTER); p4.add(p9,BorderLayout.SOUTH); p4.setBorder(new TitledBorder("文件选择项")); JPanel p5=new JPanel(); p5.setLayout(new GridLayout(2,1)); p5.add(splitByAvg); p5.add(splitBySize); JPanel p6=new JPanel(); p6.setLayout(new GridLayout(1,2)); p6.add(showAvg); p6.add(showLiang1); p6.setBorder(new TitledBorder("平均分割")); JPanel p7=new JPanel(); p7.setLayout(new GridLayout(1,2)); p7.add(showSize); p7.add(showLiang2); p7.setBorder(new TitledBorder("指定大小分割")); Box b3=Box.createVerticalBox(); b3.add(p6); b3.add(Box.createGlue()); b3.add(p7); JPanel p10=new JPanel(); p10.setLayout(new FlowLayout()); p10.add(aboutUs); JPanel p8=new JPanel(); p8.add(p5,BorderLayout.NORTH); p8.add(b3,BorderLayout.CENTER); p8.add(p10,BorderLayout.SOUTH); p8.setBorder(new TitledBorder("文件分割选项")); JSplitPane splitPanel=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,p4,p8); splitPanel.setDividerLocation(300); aboutUs.addActionListener(this); splitByAvg.addActionListener(this); splitBySize.addActionListener(this); source.addActionListener(this); destination.addActionListener(this); showAvg.addActionListener(this); showSize.addActionListener(this); start.addActionListener(this); this.setLayout(new BorderLayout()); this.add(splitPanel,BorderLayout.CENTER); this.setBounds(300,250,600,200); this.setVisible(true); this.setTitle("文件切割器---created by glzlaohuai"); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String args[]) { try { UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel()); } catch(Exception e) { e.printStackTrace(); } new SplitFiles(); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="关于我们") { JOptionPane.showMessageDialog(this,glz,"GLZ天下游民会",JOptionPane.INFORMATION_MESSAGE,null); } if(e.getActionCommand()=="源文件") { fileChooser=new JFileChooser(); fileChooser.setDialogTitle("请选择源文件"); if(fileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) { sourceFile=fileChooser.getSelectedFile().getAbsoluteFile(); showSourcePath.setText(sourceFile.getAbsolutePath()); } else ; } if(e.getActionCommand()=="目标文件夹") { fileChooser=new JFileChooser(); fileChooser.setDialogTitle("请选择目标文件夹,然后在文件名处输入文件名前缀"); if(fileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION) { destinationFile=fileChooser.getSelectedFile().getAbsoluteFile(); destinationPath=fileChooser.getSelectedFile().getAbsolutePath(); showDestinationPath.setText(destinationPath); System.out.println(destinationPath); } else ; } if(e.getActionCommand()=="平均分割") { showSize.setText(null); showSize.setEditable(false); showAvg.setEditable(true); } if(e.getActionCommand()=="指定大小分割") { showAvg.setText(null); showAvg.setEditable(false); showSize.setEditable(true); } if(e.getActionCommand()=="确定") { if(design()) { split(); } } } public boolean design() { if(!sourceFile.isFile()) { JOptionPane.showMessageDialog(this,"源文件不存在","源文件不存在",JOptionPane.INFORMATION_MESSAGE,null); return false; } if(destinationFile==null) { JOptionPane.showMessageDialog(this,"请选择目标路径","目标路径错误",JOptionPane.INFORMATION_MESSAGE,null); return false; } if(splitByAvg.isSelected()) { try{ avgNum=Integer.parseInt(showAvg.getText().trim()); } catch(Exception e) { JOptionPane.showMessageDialog(this,"请输入平均分割的份数","请输入平均分割的份数",JOptionPane.INFORMATION_MESSAGE,null); return false; } } if(splitBySize.isSelected()) { try { sizeNum=Integer.parseInt(showSize.getText().trim()); } catch(Exception ex) { JOptionPane.showMessageDialog(this,"请输入指定大小","请输入指定大小",JOptionPane.INFORMATION_MESSAGE,null); return false; } } return true; } public void split() { if(splitByAvg.isSelected()) { try{ BufferedInputStream in=new BufferedInputStream(new FileInputStream(sourceFile)); int sourceFileLength=in.available(); int everyFileLength=sourceFileLength/avgNum; int theMod=sourceFileLength%avgNum; int all[]=new int[sourceFileLength]; int every[][]=new int[avgNum-1][everyFileLength]; int lastOne[]=new int[everyFileLength+theMod]; int a=0; int b=0; while((a=in.read())!=-1) { all[b]=a; b++; } a=0; b=0; int c=0; while(a<(avgNum-1)) { while(b<everyFileLength) { every[a][b]=all[c]; c++; b++; } a++; b=0; } a=0; while(c<sourceFileLength) { lastOne[a]=all[c]; c++; a++; } a=0; b=0; while(a<(avgNum-1)) { destinationFile=new File(destinationPath+a+".txt"); try { BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(destinationFile)); while(b<everyFileLength) { out.write(every[a][b]); b++; } a++; b=0; out.close(); } catch(Exception e){ e.printStackTrace(); } } destinationFile=new File(destinationPath+a+".txt"); try{ BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(destinationFile)); while(b<lastOne.length) { out.write(lastOne[b]); b++; } out.close(); } catch(Exception e){ e.printStackTrace(); } } catch(Exception e) { e.printStackTrace(); } JOptionPane.showMessageDialog(this,"分割完毕","分割完毕",JOptionPane.INFORMATION_MESSAGE,null); } if(splitBySize.isSelected()) { try{ BufferedInputStream in=new BufferedInputStream(new FileInputStream(sourceFile)); int sourceFileLength=in.available(); int everyFileLength=sizeNum*1000; int theMod=sourceFileLength%everyFileLength; avgNum=sourceFileLength/everyFileLength; int all[]=new int[sourceFileLength]; int every[][]=new int[avgNum-1][everyFileLength]; int lastOne[]=new int[everyFileLength+theMod]; int a=1; int b=0; while((a=in.read())!=-1) { all[b]=a; b++; } a=0; b=0; int c=0; while(a<(avgNum-1)) { while(b<everyFileLength) { every[a][b]=all[c]; c++; b++; } a++; b=0; } a=0; while(c<sourceFileLength) { lastOne[a]=all[c]; c++; a++; } a=0; b=0; while(a<(avgNum-1)) { destinationFile=new File(destinationPath+a+".txt"); try { BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(destinationFile)); while(b<everyFileLength) { out.write(every[a][b]); b++; } a++; b=0; out.close(); } catch(Exception e){ e.printStackTrace(); } } destinationFile=new File(destinationPath+a+".txt"); try{ BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(destinationFile)); while(b<lastOne.length) { out.write(lastOne[b]); b++; } out.close(); } catch(Exception e){ e.printStackTrace(); } } catch(Exception e) { e.printStackTrace(); } JOptionPane.showMessageDialog(this,"分割完毕","分割完毕",JOptionPane.INFORMATION_MESSAGE,null); } } }