您的位置: 首页 > 软件教程 > Java教程专题 > Java文件拆分器

Java文件拆分器

8.0分
出处:网络 时间:2011-04-26

您可能感兴趣的话题: Java  

核心提示: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");

  }

网友评论
精品软件课程
更多 >
C++是一种静态数据类型检查的,支持多重编程... [详细]
C语言是一种面向过程的计算机程序设计语言。多... [详细]
Visual Basic是一种由微软公司开发... [详细]
Oracle Database,又名Orac... [详细]
Visual Basic.NET是从 Vis... [详细]
Visual C++微软公司的C++开发工具... [详细]