Java文件拆分器 |
工具类: import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class FileSplitUtilClass { public static int data;//拆分的个数; public static String str; public static boolean split(File filePath,int number) { byte[] b=new byte[number]; str=filePath.getAbsolutePath(); int i=0;// 缔造文件 try { FileInputStream file=new FileInputStream(filePath); try { while(file.read(b)!=-1){ i++; FileOutputStream fileout=new FileOutputStream(str+i); fileout.write(b); } file.close(); FileSplitUtilClass.data=i; filePath.delete(); return true; }catch (IOException e) { // TODO Auto-generated catch block System.out.println("split error"); e.printStackTrace();return false; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("no find"); e.printStackTrace();return false; } } //////////////////////////////////////// public static void fixFile(File filePath){ byte[] b=new byte[1024]; // String str=filePath.getAbsolutePath(); int i=0;// 缔造文件 try { FileOutputStream file=new FileOutputStream(filePath); try { System.out.println(FileSplitUtilClass.data); while(true&i<FileSplitUtilClass.data){ i++; FileInputStream file1=new FileInputStream(FileSplitUtilClass.str+i); if(file1.read(b)!=-1){ file.write(b); }else{ break; } } }catch (IOException e) { // TODO Auto-generated catch block System.out.println("no file"); e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("failure"); e.printStackTrace(); } } ///////////////////// public static JFrame getJFrame(String JFrameName){ JFrame jf=new JFrame(JFrameName); Toolkit tk=Toolkit.getDefaultToolkit(); jf.setSize(500,330); jf.setLocation((tk.getScreenSize().width-jf.getWidth())/2,(tk.getScreenSize().height-jf.getHeight())/2); return jf; } ////////////////////////// public static JPanel getJButton(String JButtonName1,String JButtonName2,Object obj){ JPanel jp=new JPanel(new FlowLayout()); JButton jb1=new JButton(JButtonName1); jb1.addActionListener((ActionListener) obj); JButton jb2=new JButton(JButtonName2); jb2.addActionListener((ActionListener)obj); jp.add(jb1); jp.add(jb2); return jp; } /////////////////////////////////////// } 检测类: import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Split implements ActionListener { JFrame jf;JTextField jt;int number; public Split(){ jf=FileSplitUtilClass.getJFrame("Filesplit"); JPanel jp=FileSplitUtilClass.getJButton("拆分","组合", this); jt=new JTextField(10); JLabel jl=new JLabel("输入字节大小"); JPanel jp1=new JPanel(new FlowLayout()); jp1.add(jl);jp1.add(jt); jf.add(jp1,BorderLayout.CENTER); jf.add(jp,BorderLayout.SOUTH); jf.setVisible(true); javax.swing.JOptionPane.showMessageDialog(jf,"请输入字节"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub String str=arg0.getActionCommand(); if("拆分".equals(str)){ JFileChooser jfc=new JFileChooser(); jfc.showOpenDialog(null);// 留神在这里的null 示意对话框弹出的位置; if(jfc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){ File filePath=jfc.getSelectedFile(); if(FileSplitUtilClass.split(filePath,Integer.parseInt(jt.getText()))){ javax.swing.JOptionPane.showMessageDialog(jf,"ok"); }else{ javax.swing.JOptionPane.showMessageDialog(jf,"fail"); }
|