java文件拆分_Java文件拆分器

您可能感兴趣的话题:

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

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");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值